Creating a uitextview scroll programmatically

I want my uitextview scroll automatically whenever the application starts. Can someone help me with the detailed code? I am new to the iPhone SDK.

+6
source share
2 answers

.h file

 @interface Credits : UIViewController { NSTimer *scrollingTimer; IBOutlet UITextView *textView; } @property (nonatomic , retain) IBOutlet UITextView *textView; - (IBAction) buttonClicked ; - (void) autoscrollTimerFired; @end 

.m file

 - (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); } 

Hope this helps you ....

+12
source

UITextView comes from UIScrollview, so you can set the scroll position using -setContentOffset: animated :.

Assuming you want to smoothly scroll at a speed of 10 points per second, you would do something like this.

 - (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]; } 

Of course, you need to set the timer and don't forget to cancel scrolling when the view disappears, etc.

+1
source

Source: https://habr.com/ru/post/896378/


All Articles