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

Posted

技术标签:

【中文标题】如何禁用 UIScrollView 到平移手势识别器?【英文标题】:How to disable UIScrollView to a pan gesture recognizer? 【发布时间】:2017-09-01 21:33:43 【问题描述】:

当调用平移手势识别器(我们称之为SomePanGestureRecognizer)时,如何禁用或强制失败UIScrollView 中的手势识别器?

gestureRecognizer(_:shouldBeRequiredToFailBy:) 方法采用手势识别器参数,而不是平移手势识别器,我不知道这是否是我无法让它工作的原因,还是因为我犯了语法错误。

我有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

    


这是平移手势识别器,需要强制UIScrollView 中的手势识别器失败:

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

更新 我将 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
        

    


更新 2 这也有效,但我不知道为什么,因为我觉得它不应该。不应该反过来吗?

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

    if otherGestureRecognizer is CustomPanGestureRecognizer 
        return true
     else 
        return false
    


【问题讨论】:

UIPanGestureRecognizersUIGestureRecognizer 的子类。这不是问题。 【参考方案1】:

我相信这就是您要找的:

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

【讨论】:

编译错误:SomePanGestureRecognizer 是一个未声明的类型 这是您指定的类。使用你自己的子类。 我希望 UIScrollView 中未命名的手势识别器无法平移手势识别器 SomePanGestureRecognizer。我们不能在同一个页面上,因为这行不通。 我认为这里混淆的根源是 SomePanGestureRecognizer 是一个实例变量,而不是一个类。我认为 PeejWeej 可能已经看到它大写并认为它是一个类。 我对它进行了子类化,但不幸的是,它仍然不起作用。

以上是关于如何禁用 UIScrollView 到平移手势识别器?的主要内容,如果未能解决你的问题,请参考以下文章

Swift 3:UIScrollView 和(禁用)手势识别器

是否可以禁用滑动手势并仅使平移手势在滚动视图中工作?

SWRevealViewController 平移手势识别器问题

如果检测到超出范围,则禁用平移手势

如何在 IO (Xamarin.IO) 中动态启用和滚动 UIScrollView?

如何区分 UIScrollView 中的平移和滚动