简体   繁体   中英

How to disable UIScrollView to a pan gesture recognizer?

How do I disable, or force to fail, the gesture recognizer in a UIScrollView when a pan gesture recognizer, lets call it SomePanGestureRecognizer , is called?

The gestureRecognizer(_:shouldBeRequiredToFailBy:) method takes a gesture recognizer parameter, not a pan gesture recognizer and I don't know if that's why I can't get it to work or because I'm making a syntax error.

I have UIScrollView subclassed and the method used to force fail is in the delegate below:

class CustomScrollView: UIScrollView {

    override init(frame: CGRect) {
        super.init(frame: frame)

        configure()

    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        configure()

    }

    private func configure() {

        isScrollEnabled = true
        showsHorizontalScrollIndicator = false
        showsVerticalScrollIndicator = false
        isPagingEnabled = true
        bounces = false
        alwaysBounceVertical = false
        alwaysBounceHorizontal = false

    }

}

// gesture recognizer delegate
extension CustomScrollView: UIGestureRecognizerDelegate {   

    // allow simultaneous gestures
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }

    // force failure
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {

        // TLDR: disable uiscrollview when SomePanGestureRecognizer is called

    }

}


And here is the pan gesture recognizer that needs to force the gesture recognizer in the UIScrollView to fail:

func addSomePanGestureRecognizer() {
    SomePanGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(oneBPanGestureHandler(gesture:)))
    oneBHandle.addGestureRecognizer(SomePanGestureRecognizer)
}



UPDATE I subclassed the UIPanGestureRecognizer and got it to work using the simultaneous delegate but I fear that this is a shaky way to do it because I'm relying on UIKit to always disable the UIScrollView before the custom pan gesture.

// gesture recognizer delegate
extension CustomScrollView: UIGestureRecognizerDelegate {

    // simultaneous gestures
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {

        if otherGestureRecognizer is CustomPanGestureRecognizer {
            return false
        } else {
            return true
        }

    }

}


UPDATE 2 This also works but I don't know why because I feel like it shouldn't. Shouldn't it be the other way around?

// force failure
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {

    if otherGestureRecognizer is CustomPanGestureRecognizer {
        return true
    } else {
        return false
    }

}

I believe this is what you're looking for:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
   let isPanAndOtherRecognizer = gestureRecognizer is UIPanGestureRecognizer && otherGestureRecognizer is SomePanGestureRecognizer
   return !isPanAndOtherRecognizer
}

// force failure
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {

   let isPanAndOtherRecognizer = gestureRecognizer is UIPanGestureRecognizer && otherGestureRecognizer is SomePanGestureRecognizer
   return isPanAndOtherRecognizer
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM