我希望滚动视图(或 textView)自动滚动,就像显示电影的演职员表时一样。我尝试了一些方法,但没有一个成功,我似乎找不到足够的答案,任何帮助将不胜感激!
提前致谢!
我希望滚动视图(或 textView)自动滚动,就像显示电影的演职员表时一样。我尝试了一些方法,但没有一个成功,我似乎找不到足够的答案,任何帮助将不胜感激!
提前致谢!
此代码将UIScrollView
在 10 秒内滚动您的 100pt:
self.scrollView.scrollEnabled = NO;
CGFloat scrollHeight = 100;
[UIView animateWithDuration:10
delay:0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^{
self.scrollView.contentOffset = CGPointMake(0, scrollHeight);
}
completion:nil];
试试这个。
[your table-name scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:pageval inSection:0] atScrollPosition:0 animated:YES];
在indexPathForRow
您可以传递您的行号并将其递增到行尾。
我认为这会奏效。
希望这会有所帮助
**
**
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.
}
}
我将它与NSTimer
对象一起使用。希望这会有所帮助。
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
}
}
}
上面将自动滚动滚动视图并在它到达结束时循环它。