使用UITableViewDropCoordinator通过具有不同数据源的dropDelegate对滴管进行动画处理



在iOS 11中,苹果向TableViews引入了原生拖放,为常见的拖放交互提供了特定的动画。假设您返回了正确的UIDropProposal,它将很容易在表视图中动画化重新排序的投递

func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
// usual code to handle drop, get dragItem, etc
// ....
// update the dataModel to reflect the change
self?.model.updateDataSourceForDrag(from: sourceIndexPath, to: destinationIndexPath)
// perform the drop animation with the drop coordinator
coordinator.drop(dragItem, toRowAt: destinationIndexPath)
}

这将很好地将放置的项目插入到tableView中,并将其插入到悬停的间隙中。

快进到iOS 13+和不同数据源的使用,我在苹果文档中找不到使用dropCoordinator和快照的参考,也没有更新指南、教程或WWDC视频来展示如何组合这两组API。

液滴控制器将正确地";管理";tableView,并四处移动单元格以显示拖动单元格将掉落的间隙,但它不会使用coordinator.drop(dragItem, toRowAt: destinationIndexPath)设置实际掉落的动画。

我目前的解决方法是手动更新,然后应用快照:

DispatchQueue.main.async {
var snapshot = self?.dataSource.snapshot()
if destinationIndexPath.row > sourceIndexPath.row {
snapshot?.moveItem((self?.dataSource.itemIdentifier(for: sourceIndexPath))!, afterItem: (self?.dataSource.itemIdentifier(for: destinationIndexPath))!)
} else {
snapshot?.moveItem((self?.dataSource.itemIdentifier(for: sourceIndexPath))!, beforeItem: (self?.dataSource.itemIdentifier(for: destinationIndexPath))!)
}
self?.dataSource.apply(snapshot!, animatingDifferences: false)
}

这在一定程度上起作用,但不会与放置控制器动画集成。因此,当我拖动时,我可以在tableView中获得提供放置间隙的动画,但一旦我放置它,它就会使单元从其原始indexPath移动,而不是从已设置动画的已放置项目移动(正如我所期望的那样(,这一切都相当笨拙。

dropCoordinator可以调整tableView以设置拖动的动画,这表明它也应该能够设置拖放的动画,但我找不到实现这一点的方法。

如果有任何经验,我们将不胜感激(在我放弃并将代码恢复到旧的UITableViewDataSource方法之前(。

放置动画现在可以在几个月没有修改的代码上正常工作。

iOS14和早期版本的iOS15中一定有一个错误,在最近的一次更新中修复了这个错误。

所有这些都浪费了时间和精力。有时我们真的应该对自己的代码更有信心。

最新更新