在我的viewController中,我有一个CollectionView:mainCollectionView
作为UIView子视图,其中包含三个单元格(viewController框架大小),它们可以水平滚动。 mainCollectionView
还有一个panGesture
,它显示了另一个viewController:sideMenuController
。
在我的mainCollectionView
单元内是另一个水平滚动collectionView:weekCollectionView
作为子视图。当我在panGesture
中滚动时,如何禁用 mainCollectionView
和weekCollectionView
滚动?
这是我的代码-
mainCollectionView panGesture:
fileprivate func setupPanGesture() {
panGesture = UIPanGestureRecognizer(target: self, action: #selector(panRight(sender:)))
panGesture.delegate = self
mainCollectionView.addGestureRecognizer(panGesture)
}
@objc func panRight(sender: UIPanGestureRecognizer) {
let translation = sender.translation(in: mainCollectionView)
let indexPath = NSIndexPath(item: 0, section: 0)
if (mainCollectionView.cellForItem(at: indexPath as IndexPath) != nil) {
if translation.x > 0 {
(UIApplication.shared.keyWindow?.rootViewController as? MasterViewController)?.handlePan(sender: sender)
mainCollectionView.isScrollEnabled = false
} else if translation.y > 0 || translation.y < 0 {
panGesture.isEnabled = false
mainCollectionView.isScrollEnabled = true
}
}
if (mainCollectionView.cellForItem(at: indexPath as IndexPath) == nil) {
panGesture.isEnabled = false
mainCollectionView.isScrollEnabled = true
}
panGesture.isEnabled = true
mainCollectionView.isScrollEnabled = true
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
答案 0 :(得分:0)
对您的课程实施 UIGestureRecognizerDelegate
将手势委托设置为自己。
panGesture.delegate = self
添加此委托功能
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if (gestureRecognizer is UIPanGestureRecognizer || gestureRecognizer is UIRotationGestureRecognizer) {
return true
} else {
return false
}
}