How to scroll text in UITextView like teleprompter in swift?

399 views Asked by At

I want to scroll my text like teleprompter scroll. I've read about UIDynamicAnimation but not clear with UITextView for multiple lines.

Appreciate your suggestions or code snippets to achieve this.

1

There are 1 answers

3
Deryck Lucian On

Using a recursive method we could animate each row scroll using UIView.animate(withDuration:animations:completed) till we reach the end of the textView.

func animateRowWith(duration: TimeInterval, rowHeight: CGFloat) {
    if self.textView.contentOffset.y < self.textView.contentSize.height - self.textView.frame.size.height {
        UIView.animate(withDuration: duration, animations: {
            self.textView.contentOffset = CGPoint(x: self.textView.contentOffset.x, y: self.textView.contentOffset.y + rowHeight)
        }) { (_) in
            self.animateRowWith(duration: duration, rowHeight: rowHeight)
        }
    }
}

calling it:

animateRowWith(duration: 1.0, rowHeight: self.textView.font?.lineHeight ?? 0).

Note that you will have a delay after each row has scrolled.