0

我想像提词器滚动一样滚动我的文本。我已经阅读了多行UIDynamicAnimation但不清楚UITextView

感谢您的建议或代码片段来实现这一点。

4

1 回答 1

1

使用递归方法,我们可以为每一行滚动设置动画,UIView.animate(withDuration:animations:completed)直到到达 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)
        }
    }
}

调用它:

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

请注意,每行滚动后都会有延迟。

于 2018-11-19T01:34:18.970 回答