【问题标题】:CollectionView Drag and Drop when embedded in PageViewController嵌入 PageViewController 时的 CollectionView 拖放
【发布时间】:2020-12-11 02:55:36
【问题描述】:
当对嵌入在 UIPageViewController 中的 UICollectionView 使用拖放操作时,代理在分页时会立即触发 didExit。
- 开始拖放手势
- 页面到新视图
- CollectionDropDelegate 立即触发:
newCV.didEnter
oldCV.didExit
newCV.didExit
newCV.didUpdate 永远不会被调用。如果我们在这一点上放开下降,它就会取消。我的 PageViewController 不是全屏的,所以如果我将拖动移到外面再移回,我仍然可以在分页后执行拖放,但这是一个糟糕的用户体验。
注意事项:
- 不使用 UICollectionViewController
- CollectionView 被添加到
viewDidLoad 中的 UIViewController 层次结构中
有什么想法吗?
【问题讨论】:
标签:
ios
uicollectionview
uipageviewcontroller
uicollectionviewdropdelegate
【解决方案1】:
我可以通过切换collectionView.isUserInteractionEnabled 来解决这个问题。弄清楚在生命周期中的哪个位置执行此操作有点挑战性,最终我最终使用了 UIPageViewControllerDelegate didFinishAnimating...
所以在我的ContentViewController.viewDidLoad 中,我设置了collectionView.isUserInteractionEnabled = false,然后在委托中,我需要有条件地启用/禁用集合视图。
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if completed {
guard let showing = pageViewController.viewControllers?.first as? DayViewController else {
assertionFailure()
return
}
// Disable All
previousViewControllers.forEach {
if let dayVC = $0 as? DayViewController {
dayVC.collectionView?.isUserInteractionEnabled = false
}
}
// Enable Showing
showing.collectionView?.isUserInteractionEnabled = true
} else {
guard let showing = previousViewControllers.first as? DayViewController else {
assertionFailure()
return
}
// Disable All
viewControllers?.forEach {
if let dayVC = $0 as? DayViewController {
dayVC.collectionView?.isUserInteractionEnabled = false
}
}
// Enable Showing
showing.collectionView?.isUserInteractionEnabled = true
}
}