Reputation: 424
I want a scroll view (or textView) to scroll automatically, like when the credits of a movie is shown. I have tried a few methods, but none came through and I cannot seem to find a sufficient answer, any help would be greatly appreciated!!
Thanks in advance!
Upvotes: 3
Views: 5309
Reputation: 10346
class AutoScrollView: UIScrollView {
var timer: Timer?
override func didMoveToSuperview() {
super.didMoveToSuperview()
timer?.invalidate()
timer = Timer.scheduledTimer(timeInterval: 1.0 / 30.0, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
}
@objc func timerAction() {
contentOffset.y = contentOffset.y + 1
if contentOffset.y >= contentSize.height - bounds.height {
contentOffset.y = 0
}
}
}
Above will automatically scroll the scrollview and loop it when it reaches end.
Upvotes: 1
Reputation: 10959
Try this.
[your table-name scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:pageval inSection:0] atScrollPosition:0 animated:YES];
In indexPathForRow
you can pass your row number and increment it till the end of row.
I think that will do the trick.
Hope this will help
**
**
count=txtvw.frame.size.height; //integer counter initialized as textview's height
//call "scrolltextview" method at regular time interval. (here it is calling method in 0.3 second timespan)
self.timer = [NSTimer scheduledTimerWithTimeInterval:.3f
target:self
selector:@selector(scrolltextview)
userInfo:nil
repeats:YES];
-(void)scrolltextview
{
//iterate "count" every time the method is called by the lineheight of textview's font.
count=count+ txtvw.font.lineHeight;
if(count<=txtvw.text.length)
{
NSRange range = NSMakeRange(count - 1, 1);
[txtvw scrollRangeToVisible:range]; // scroll with range
}
else {
[timer invalidate]; // if count match with the condition than invalidate the timer so method not called now.
}
}
I use this with NSTimer
object. Hope this will help.
Upvotes: 1
Reputation: 9345
This code will scroll your UIScrollView
100pt in 10 seconds:
self.scrollView.scrollEnabled = NO;
CGFloat scrollHeight = 100;
[UIView animateWithDuration:10
delay:0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^{
self.scrollView.contentOffset = CGPointMake(0, scrollHeight);
}
completion:nil];
Upvotes: 4