具有自动滚动功能的ScrollView

时间:2012-09-20 03:29:09

标签: iphone ios xcode uiscrollview

我希望滚动视图(或textView)能够自动滚动,就像显示电影的片尾时一样。我尝试过几种方法,但没有一种方法可以通过,我似乎找不到足够的答案,任何帮助都会非常感激!!

提前致谢!

3 个答案:

答案 0 :(得分:4)

此代码将在10秒内滚动UIScrollView 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];

答案 1 :(得分:1)

试试这个。

[your table-name scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:pageval inSection:0] atScrollPosition:0 animated:YES];

indexPathForRow中,您可以传递行号并将其递增至行尾。

我认为这样可以解决问题。

希望这会有所帮助

**

当您使用textview时,可以试试这个

**

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对象中使用它。希望这会有所帮助。

答案 2 :(得分:0)

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
        }
    }
}

上方将自动滚动滚动视图,并在滚动视图结束时循环播放。