禁用uicollectionview水平滚动的外部平移手势

时间:2018-11-07 12:45:40

标签: ios iphone swift uicollectionview uipangesturerecognizer

在我的viewController中,我有一个CollectionView:mainCollectionView作为UIView子视图,其中包含三个单元格(viewController框架大小),它们可以水平滚动。 mainCollectionView还有一个panGesture,它显示了另一个viewController:sideMenuController

在我的mainCollectionView单元内是另一个水平滚动collectionView:weekCollectionView作为子视图。当我在panGesture中滚动时,如何禁用 mainCollectionViewweekCollectionView滚动?

这是我的代码-

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
}

1 个答案:

答案 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
    }
}