Swift 4:我试图在点击按钮时滚动 textView。这是我的代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var txtViewStory: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
}
var scrollingTimer = Timer()
@IBAction func scroll(_ sender: UIButton) {
scrollingTimer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(ViewController.startScrolling), userInfo: nil, repeats: true)
scrollingTimer.fire()
}
@objc func startScrolling(){
var currentOffset = txtViewStory.contentOffset
currentOffset = CGPoint(x: currentOffset.x, y: currentOffset.y + 1)
txtViewStory.setContentOffset(currentOffset, animated: false)
}
}
它的样子是这样的:
当 TextView 滚动到底部时动画将停止。但我希望它一次又一次地重复滚动。有什么办法让它发挥作用吗?如有任何建议,我们将不胜感激。
最佳答案
非常肮脏的代码,但应该可以工作;>
@IBAction func scroll(_ sender: UIButton) {
scrollingTimer.invaldiate()
scrollingTimer = nil
txtViewStory.setContentOffset(CGPoint(x: currentOffset.x, y: 0), animated: false)
scrollingTimer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(ViewController.startScrolling), userInfo: nil, repeats: true)
scrollingTimer.fire()
}
关于ios - 如何使用 Swift 滚动 TextView 时重复动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46977326/