gpt4 book ai didi

swift - 如何在 UITextView 中像提词器一样在 swift 中滚动文本?

转载 作者:行者123 更新时间:2023-11-28 14:03:03 24 4
gpt4 key购买 nike

我想像提词器滚动一样滚动我的文本。我已经阅读了有关 UIDynamicAnimation 的内容,但对于多行的 UITextView 并不清楚。

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

最佳答案

使用递归方法,我们可以使用 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)

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

关于swift - 如何在 UITextView 中像提词器一样在 swift 中滚动文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53350715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com