當平局手勢識別器被稱爲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
}
}
'UIPanGestureRecognizers'是UIGestureRecognizer'的'子類。這不是問題。 – PeejWeej