我试图弄清楚如何通过 xcode/swift 中的后台线程滚动我的表格视图。这是我到目前为止的代码:
func startBackgroundScrollingThread() {
DispatchQueue.global(qos: .background).async {
while self.scrollingMode {
for row in 0..<self.list.count {
DispatchQueue.main.async {
if self.scrollingMode {
Thread.sleep(forTimeInterval: 0.2)
let indexPath = IndexPath(row: row, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .none, animated: true)
}
}
Thread.sleep(forTimeInterval: 0.2)
}
for row in (0..<self.list.count).reversed() {
DispatchQueue.main.async {
if self.scrollingMode {
Thread.sleep(forTimeInterval: 0.2)
let indexPath = IndexPath(row: row, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .none, animated: true)
}
}
Thread.sleep(forTimeInterval: 0.5)
}
}
}
}
这段代码有点工作,但它不是一个平滑的滚动。这是非常简陋的。理想情况下,我希望它能慢慢地下降到视野中,然后一次又一次地返回。我感谢任何帮助!
评论这一行
Thread.sleep(forTimeInterval: 0.2)