2011-09-01 10 views
6

Quiero hacer que mi uitextview se desplace automáticamente cada vez que se inicie la aplicación. ¿Alguien puede ayudarme con un código detallado? Soy nuevo en iPhone SDK.Hacer uitextview desplazarse programáticamente

+0

cómo es exactamente lo que usted quiere que desplazarse? ¿Quieres que se desplace hasta el final o hasta un punto intermedio? ¿O quieres que se desplace lentamente de arriba hacia abajo? – mahboudz

+0

posible duplicado de http://stackoverflow.com/questions/1088960/iphone-auto-scroll-uitextview-but-allow-manual-scrolling-also – tipycalFlow

+0

quiero desplazar el uitextview de arriba a abajo, lentamente. y no hay interacción permitida para la vista de texto. quiero decir, el usuario no puede editar nada en la vista de texto. – user919050

Respuesta

12

archivo .h

@interface Credits : UIViewController 
{ 
    NSTimer *scrollingTimer; 

    IBOutlet UITextView *textView; 


} 
@property (nonatomic , retain) IBOutlet UITextView *textView; 

- (IBAction) buttonClicked ; 

- (void) autoscrollTimerFired; 

@end 

archivo .m

- (void) viewDidLoad 
{  
    // it prints the initial position of text view 
    NSLog(@"%f %f",textView.contentSize.width , textView.contentSize.height); 

    if (scrollingTimer == nil) 
    { 
     // A timer that updates the content off set after some time so it can scroll 
     // you can change time interval according to your need (0.06) 
     // autoscrollTimerFired is the method that will be called after specified time interval. This method will change the content off set of text view 
     scrollingTimer = [NSTimer scheduledTimerWithTimeInterval:(0.06) 
         target:self selector:@selector(autoscrollTimerFired) userInfo:nil repeats:YES];   
    } 
} 

- (void) autoscrollTimerFired 
{ 
    CGPoint scrollPoint = self.textView.contentOffset; // initial and after update 
    NSLog(@"%.2f %.2f",scrollPoint.x,scrollPoint.y); 
    if (scrollPoint.y == 583) // to stop at specific position 
    { 
     [scrollingTimer invalidate]; 
     scrollingTimer = nil; 
    } 
    scrollPoint = CGPointMake(scrollPoint.x, scrollPoint.y + 1); // makes scroll 
    [self.textView setContentOffset:scrollPoint animated:NO]; 
    NSLog(@"%f %f",textView.contentSize.width , textView.contentSize.height); 

} 

espero que le ayuda ....

+0

sí ... las variables que necesita utilizar en muchos lugares en el archivo .m .. en el código anterior scrollingTimer, textView tiene que declarar en el archivo .h – Maulik

+0

estaré disponible en el flujo de StackOver ..: D – Maulik

+1

Gracias por la solución.Desarrollé un teclado personalizado y para esto su código me ayudó en UITextView. –

1

UITextView se deriva de UIScrollview para que pueda establecer la posición de desplazamiento usando -setContentOffset: animated :.

Suponiendo que desea desplazarse sin problemas a la velocidad de 10 puntos por segundo, haría algo como eso.

- (void) scrollStepAnimated:(NSTimer *)timer { 
    CGFloat scrollingSpeed = 10.0; // 10 points per second 
    NSTimeInterval repeatInterval = [timer timeInterval]; // ideally, something like 1/30 or 1/10 for a smooth animation 

    CGPoint newContentOffset = CGPointMake(self.textView.contentOffset.x, self.textView.contentOffset.y + scrollingSpeed * repeatInterval); 
    [self.textView setContentOffset:newContentOffset animated:YES]; 
} 

Por supuesto, tiene que configurar el temporizador y asegurarse de cancelar el desplazamiento cuando la vista desaparece, y así sucesivamente.

+0

Debe declarar el NSTimer como variable de instancia y configurarlo con un intervalo de repetición atractivo en -viewWindowAppearAnimated :. A continuación, asegúrese de invalidar y liberarlo en -dealloc y -viewWillDisappearAnimated :. –

+0

No puedo darte el código completo, porque no estoy seguro de que entiendas qué está haciendo el código en ese momento. Asegúrese de tratar de entender la lógica detrás de ese código. Lo que está haciendo es simplemente desplazarse hacia la parte inferior dada una velocidad específica. –

+0

En su archivo .h tiene que hacer algo como esto donde declare sus otras variables de instancia: NSTimer * scrollingTimer; @Maulik le mostró cómo configurar e invalidar el temporizador. –

Cuestiones relacionadas