2017-09-01 100 views
0

當平局手勢識別器被稱爲SomePanGestureRecognizer時,如何禁用或強制手勢識別器在UIScrollView中失敗?如何禁用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) 
} 



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 
     } 

    } 

} 


更新2這也適用,但我不知道爲什麼,因爲我覺得它不應該。難道不是相反嗎?

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

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

} 
+0

'UIPanGestureRecognizers'是UIGestureRecognizer'的'子類。這不是問題。 – PeejWeej

回答

0

我相信這是你在找什麼:

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 
} 
+0

編譯錯誤:SomePanGestureRecognizer是一個未聲明的類型 – sconewolf

+0

這是您指定的類。使用任何你自己的子類。 – PeejWeej

+0

我希望UIScrollView中的未命名的手勢識別器無法使用平移手勢識別器SomePanGestureRecognizer。我們不能在同一頁面上,因爲這不起作用。 – sconewolf

相關問題