我有一个NSTableView,间歇性地停止动画并正确更新,导致糟糕的用户体验。
let oldRows = filteredDocuments
let newRows = newFilteredDocuments
let diff = oldRows.diff(newRows)
filteredDocuments = newFilteredDocuments
if (diff.results.count > 0) {
let deletionIndexPaths = NSMutableIndexSet()
diff.deletions.forEach { deletionIndexPaths.addIndex($0.idx) }
let insertionIndexPaths = NSMutableIndexSet()
diff.insertions.forEach { insertionIndexPaths.addIndex($0.idx) }
self.tableView?.beginUpdates()
self.tableView?.removeRowsAtIndexes(deletionIndexPaths, withAnimation: NSTableViewAnimationOptions.EffectFade)
self.tableView?.insertRowsAtIndexes(insertionIndexPaths, withAnimation: NSTableViewAnimationOptions.SlideLeft)
self.tableView?.endUpdates()
}
当它停止制作动画似乎没有任何逻辑,并且在我完成的许多测试中感觉它几乎与它的构建相关。有趣的是,当我分析时,它永远不会停止动画......
好像主线程上的东西堵塞了UI,然后NSTableView超时并取消了更新 - 但我不知道如何调试它。
答案 0 :(得分:1)
如果你做一个然后另一个怎么样?我刚刚在需要插入单元格和滚动时做了类似的事情,导致用户体验不佳。解决方案是增加一点延迟。不确定这是否是您尝试根据小代码段完成的内容,但我们继续:
let oldRows = filteredDocuments
let newRows = newFilteredDocuments
let diff = oldRows.diff(newRows)
filteredDocuments = newFilteredDocuments
if (diff.results.count > 0) {
let deletionIndexPaths = NSMutableIndexSet()
diff.deletions.forEach { deletionIndexPaths.addIndex($0.idx) }
let insertionIndexPaths = NSMutableIndexSet()
diff.insertions.forEach { insertionIndexPaths.addIndex($0.idx) }
self.tableView?.beginUpdates()
self.tableView?.removeRowsAtIndexes(deletionIndexPaths, withAnimation: NSTableViewAnimationOptions.EffectFade)
self.tableView?.endUpdates()
let delay = 0.35
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC))) // Hate this syntax
dispatch_after(delay, dispatch_get_main_queue(), { [weak self] in
self?.tableView?.beginUpdates()
self?.tableView?.insertRowsAtIndexes(insertionIndexPaths, withAnimation: NSTableViewAnimationOptions.SlideLeft)
self?.tableView?.endUpdates()
})
}