简体   繁体   English

如何禁用UIScrollView到平移手势识别器?

[英]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? 当平移手势识别器被称为SomePanGestureRecognizer调用时,如何在UIScrollView禁用或强制失败?

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. gestureRecognizer(_:shouldBeRequiredToFailBy:)方法采用手势识别器参数,而不是平移手势识别器参数,而且我不知道这是为什么无法使其正常工作或由于语法错误。

I have UIScrollView subclassed and the method used to force fail is in the delegate below: 我有UIScrollView子类,用于强制失败的方法在下面的委托中:

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: 这是平移手势识别器,需要强制UIScrollView的手势识别器失败:

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. UPDATE我将UIPanGestureRecognizer细分为子类,并使用同时委托使它工作,但是我担心这是一种摇晃的方法,因为我依赖UIKit始终在自定义平移手势之前禁用UIScrollView。

// 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. 更新2这也有效,但我不知道为什么,因为我觉得不应该这样做。 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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