我花了很多时间来找出这个错误。一开始滚动看起来不错,但不时滚动增加自动,这使人类无法阅读。如何像开始时一样保持滚动。
var start_index = 0
var dest_index = 0 //approximately 300
var timer = Timer()
var falagisTrue : Bool?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
dest_index = newsViewModels.count
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.keepScrolling), userInfo: nil, repeats: true)
}
@objc func keepScrolling(){
if start_index > dest_index - 2{
timer.invalidate()
}else{
if (falagisTrue ?? false || falagisTrue == nil) {
falagisTrue = false
start_index += 1
UIView.animate(withDuration: 10, delay: 0, options: [], animations: {
let Index = IndexPath(row: self.start_index, section: 0)
self.tableView.scrollToRow(at:Index, at: .top, animated: false)
}) { finished in
self.falagisTrue = true
}
}
}
}
你能帮我修复这个错误还是提供任何替代解决方案来保持自动滚动?
我尝试了你的代码,它对我来说工作正常。但:
您可以添加动画选项 .curveLiner(动画将在整个持续时间内均匀发生)。可能是你的错误:
UIView.animate(withDuration: 10, delay: 0, options: [.curveLinear], animations: {
let Index = IndexPath(row: self.start_index, section: 0)
self.tableView.scrollToRow(at:Index, at: .top, animated: false)
}) { finished in
self.falagisTrue = true
}
您应该在视图消失时使计时器失效(不同的视图在旧计时器工作时每次都会创建额外的计时器):
override func viewWillDisappear(_ animated: Bool) {
timer.invalidate()
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句