【问题标题】:ScrollView with automatic scrolling feature具有自动滚动功能的 ScrollView
【发布时间】:2012-09-20 03:29:08
【问题描述】:

我希望滚动视图(或 textView)自动滚动,就像显示电影的演职员表时一样。我已经尝试了一些方法,但没有一个成功,我似乎找不到足够的答案,任何帮助将不胜感激!

提前致谢!

【问题讨论】:

  • 你可以循环直到你的 contentSize 达到它的结尾,然后 scrollToCGPoint。

标签: iphone ios xcode uiscrollview


【解决方案1】:

此代码将在 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];

【讨论】:

    【解决方案2】:

    试试这个。

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

    【讨论】:

    • 谢谢,但我真的不想通过表格视图来实现这一点!
    【解决方案3】:
    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
            }
        }
    }
    

    上面会自动滚动滚动视图并在到达结束时循环它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多