3

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!

iProRage
  • 424
  • 7
  • 19

3 Answers3

4

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];
Andreas Ley
  • 9,109
  • 1
  • 47
  • 57
1

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

**

As you are using textview you can try this

**

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.

KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
1
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.

Leszek Szary
  • 9,763
  • 4
  • 55
  • 62
  • This seems like an acceptable approach, but when I call this in a SwiftUI UIViewRepresentable wrapper, it does not seem to scroll (there's no compile-time errors though) – Kelvin Jou Dec 27 '22 at 07:45