如果目标是让平移手势/s 阻止识别滑动(通常情况):
swipeGesture.requireGestureRecognizerToFail(panGesture)
如果目标是让滑动手势/秒阻止识别平移(不寻常的情况):
panGesture.requireGestureRecognizerToFail(swipeGesture)
也照常做:
panGesture.delegate = self
swipeGesture.delegate = self
并实现shouldRecognizeSimultaneouslyWithGestureRecognizer
始终返回true
注意:UIWebView
在's 和' 之类的东西上,UIScrollView
要正确解释单点触摸滑动,最好使用以下requireGestureRecognizerToFail
三 (3) 个:平移、长按和第二个滑动识别器,.numberOfTouchesRequired = 2
以便 UX 直观地工作。
这是我们使用的(Swift):
class AboutViewController: UIViewController, UIGestureRecognizerDelegate {
@IBOutlet var webView: UIWebView!
var swipeToDismiss: UISwipeGestureRecognizer {
var r = UISwipeGestureRecognizer(target: self, action: "dismiss:")
r.direction = .Right
r.delegate = self
r.requireGestureRecognizerToFail(longPress)
r.requireGestureRecognizerToFail(pan)
r.requireGestureRecognizerToFail(doubleSwipe)
return r
}
var doubleSwipe: UISwipeGestureRecognizer {
var r = UISwipeGestureRecognizer()
r.direction = .Right
r.delegate = self
r.numberOfTouchesRequired = 2
return r
}
var longPress: UILongPressGestureRecognizer {
var r = UILongPressGestureRecognizer()
r.allowableMovement = 4000.0
r.delegate = self
return r
}
var pan: UIPanGestureRecognizer {
var r = UIPanGestureRecognizer()
r.delegate = self
return r
}
func dismiss(recognizer: UISwipeGestureRecognizer!) {
// ...popViewControllerAnimated(true)
}
private func setupWebView() {
// ...
webView.addGestureRecognizer(longPress)
webView.addGestureRecognizer(swipeToDismiss)
webView.addGestureRecognizer(pan)
webView.addGestureRecognizer(doubleSwipe)
}
// MARK: - UIGestureRecognizerDelegate
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
println("gesture start: \(gestureRecognizer)")
return true
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
println("multiple gestures: \(gestureRecognizer), \(otherGestureRecognizer)")
return true
}
// MARK: - UIViewController
override func viewDidLoad() {
super.viewDidLoad()
setupWebView()
}
}