简体   繁体   中英

How to scroll text in UITextView like teleprompter in swift?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM