khalilAli
khalilAli

Reputation: 1

disable external pan gesture for uicollectionview horizontal scroll

In my viewController i have a CollectionView: mainCollectionView as a UIView subview with three cells (viewController frame size) that scroll horizontally. The mainCollectionView also has a panGesture that shows another viewController: sideMenuController.

Within my mainCollectionView Cell is another horizontal scrolling collectionView: weekCollectionView as a subview. How would I disable the panGesture and the mainCollectionView scroll when im scrolling in the weekCollectionView?

here is my code -

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
}

Upvotes: 0

Views: 1435

Answers (1)

user1376400
user1376400

Reputation: 624

implements UIGestureRecognizerDelegate to your class

set gesture delegate to self.

panGesture.delegate = self

Add this Delegate function

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    if (gestureRecognizer is UIPanGestureRecognizer || gestureRecognizer is UIRotationGestureRecognizer) {
        return true
    } else {
        return false
    }
}

Upvotes: 2

Related Questions