I want to make my uitextview
to scroll automatically whenever the application is launched. Can anyone help me with a detailed code? I am new to iPhone SDK.
Asked
Active
Viewed 5,752 times
6

palacsint
- 28,416
- 10
- 82
- 109

user919050
- 71
- 1
- 5
-
How exactly do you want it to scroll? Do you want it to scroll to the end, or to a spot in the middle? Or do you want it to scroll slowly from top to bottom? – mahboudz Sep 01 '11 at 06:34
-
possible duplicate of http://stackoverflow.com/questions/1088960/iphone-auto-scroll-uitextview-but-allow-manual-scrolling-also – tipycalFlow Sep 01 '11 at 06:40
-
i want to scroll the uitextview from top to bottom, slowly. and there is no interaction allowed to the textview. i mean, the user cannot edit anything in the textview. – user919050 Sep 01 '11 at 06:40
-
well, i want a detail help with the coding part. I have already referred many questions here but none of them helped me with detail coding. pls give me a detail ans (coding expected). – user919050 Sep 01 '11 at 06:43
-
is your text static or dynamic ? – Maulik Sep 01 '11 at 07:36
2 Answers
11
.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 it helps you....

Maulik
- 19,348
- 14
- 82
- 137
-
yeah... the variables that you need to use in many places in .m file.. in above code scrollingTimer , textView u have to declare in .h file – Maulik Sep 01 '11 at 08:45
-
-
1Thanks for the solution. I developed a custom keyboard and for this your code helped me in UITextView. – Mahmud Ahsan Jul 02 '13 at 15:47
1
UITextView derives from UIScrollview so you can set the scrolling position using -setContentOffset:animated:.
Assuming you want to scroll smoothly at the speed of 10 points per second, you'd do something like that.
- (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 have to setup the timer and be sure to cancel the scrolling when the view disappears and so on.

Fabian Kreiser
- 8,307
- 1
- 34
- 60
-
You have to declare the NSTimer as instance variable and set it up with a good-looking repeat interval in -viewWillAppearAnimated:. Then make sure to invalidate and release it in -dealloc and -viewWillDisappearAnimated:. – Fabian Kreiser Sep 01 '11 at 07:15
-
I can't give you the full code, because I'm not sure you understand what the code is doing then. Be sure to try to understand the logic behind that code. What it is doing is just to scroll to the bottom given a specific speed. – Fabian Kreiser Sep 01 '11 at 07:17
-
In your .h file you have to do something like this where you declare your other instance variables: NSTimer *scrollingTimer; @Maulik showed you how to setup and invalidate the timer. – Fabian Kreiser Sep 01 '11 at 08:08
-
@Maulik's code is missing the -dealloc code. And if you setup the timer in -viewDidLoad you have to invalidate it in -viewDidUnload, too. Neither Maulik nor me did give you the full code but just some snippets. You'll have to declare the method for the timer too or you'll get a warning about an unknown selector. – Fabian Kreiser Sep 01 '11 at 08:22
-
ohk great.thats really a great help from your side.can you help me further??i can declare the timer using NSTimer. but i am not getting how to declare the "autoscrolltimerfired". from the maulik's code, i feel,to declare the scrolling timer, i need to write this in my .h file : IBOutlet NSTimer *scrollingtimer; ...right? but i am not getting how to declare the autoscrolltimerfired?or is there any need to declare this at all? – user919050 Sep 01 '11 at 08:30
-
It is not an IBOutlet, just a simple NSTimer. You declare the autoScrollTimerFired method just like any other method. You need to declare it so the compiler knows that such a method exists. – Fabian Kreiser Sep 01 '11 at 08:40
-
anyways, thank you a lot @Fabian Kreiser...i really appreciate for your help...thank you once again. – user919050 Sep 01 '11 at 08:55