kel*_*lin 6 drag-and-drop uikit ios uicollectionview swift
它具有水平布局,具有不同大小的单元格和部分。
拖动交互效果很好,但我注意到UICollectionViewDropDelegate 中有一个问题:
func collectionView(
_ collectionView: UICollectionView,
dropSessionDidUpdate session: UIDropSession,
withDestinationIndexPath destinationIndexPath: IndexPath?)
-> UICollectionViewDropProposal {
if let destination = destinationIndexPath {
print(destination) // Prints WRONG index path!!!
return UICollectionViewDropProposal(
operation: .move, intent: .insertAtDestinationIndexPath
)
}
return UICollectionViewDropProposal(
operation: .cancel, intent: .unspecified
)
}
Run Code Online (Sandbox Code Playgroud)
错误的目标索引路径被传递给collectionView(_:dropSessionDidUpdate:withDestinationIndexPath:).
因此,我无法正确确定该部分并确定该处是否可用。
kel*_*lin 10
所以,这是 UIKit 的一个错误。正确的目标索引路径可以计算如下:
func collectionView(
_ collectionView: UICollectionView,
dropSessionDidUpdate session: UIDropSession,
withDestinationIndexPath destinationIndexPath: IndexPath?)
-> UICollectionViewDropProposal {
// Calculating location in view
let location = session.location(in: collectionView)
var correctDestination: IndexPath?
// Calculate index inside performUsingPresentationValues
collectionView.performUsingPresentationValues {
correctDestination = collectionView.indexPathForItem(at: location)
}
guard let destination = correctDestination else {
return UICollectionViewDropProposal(
operation: .cancel, intent: .unspecified
)
}
// check destination
// ...
}
Run Code Online (Sandbox Code Playgroud)
为了修复这个bug,首先,我试图用的组合location(in:)和indexPathForItem(at:)。结果索引路径等于destinationIndexPath委托方法提供的。为什么?我的注意力被UIDataSourceTranslating吸引了。这是一个协议,允许集合和表格视图显示占位符单元格以进行拖放,而无需更改实际数据源。当拖放交互结束时,占位符很容易被删除。所以,我做了一个假设
destinationIndexPath 是在帮助下计算的 indexPathForItem(at:)然后我试图包装indexPathForItem(at:)成performUsingPresentationValues(_:)与接收的索引路径是正确的!
| 归档时间: |
|
| 查看次数: |
859 次 |
| 最近记录: |