我正在开发一个应用程序,其中我使用了平移手势以及滑动手势。所以每次我做划动手势的时候,都会调用来自平移手势的方法,而不会调用划动手势方法。
所有的手势方法之间有没有优先级?
发布于 2012-04-14 21:33:29
您可以通过实现UIGestureRecognizerDelegate协议的以下方法来并行调用它们:
- (BOOL)gestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UISwipeGestureRecognizer *)otherGestureRecognizer
{
return YES;
}发布于 2011-02-03 07:16:03
在UIGestureRecognizer类上有一个名为"cancelsTouchesInView“的属性,它的默认值是YES。这将导致任何挂起的手势被取消。平移手势首先被识别,因为它不需要有"touch up“事件,所以它取消了Swipe手势。
如果您希望这两个手势都被识别,请尝试添加:
[yourPanGestureInstance setCancelsTouchesInView:NO];
发布于 2018-01-13 08:27:50
优先使用swipe
您可以使用require(toFail:)方法为UIGestureRecognizer提供优先级。
@IBOutlet var myPanGestureRecognizer: UIPanGestureRecognizer!
@IBOutlet var mySwipeGestureRecognizer: UISwipeGestureRecognizer!
myPanGesture.require(toFail: mySwipeGestureRecognizer)现在,您的pan将仅在您的刷卡失败时执行。
使用 pan everything
如果滑动和平移手势识别器在此设置中玩得不好,您可以将所有逻辑滚动到平移手势识别器中,以获得更多控制。
let minHeight: CGFloat = 100
let maxHeight: CGFloat = 700
let swipeVelocity: CGFloat = 500
var previousTranslationY: CGFloat = 0
@IBOutlet weak var cardHeightConstraint: NSLayoutConstraint!
@IBAction func didPanOnCard(_ sender: Any) {
guard let panGesture = sender as? UIPanGestureRecognizer else { return }
let gestureEnded = bool(panGesture.state == UIGestureRecognizerState.ended)
let velocity = panGesture.velocity(in: self.view)
if gestureEnded && abs(velocity.y) > swipeVelocity {
handlePanOnCardAsSwipe(withVelocity: velocity.y)
} else {
handlePanOnCard(panGesture)
}
}
func handlePanOnCard(_ panGesture: UIPanGestureRecognizer) {
let translation = panGesture.translation(in: self.view)
let translationYDelta = translation.y - previousTranslationY
if abs(translationYDelta) < 1 { return } // ignore small changes
let newCardHeight = cardHeightConstraint.constant - translationYDelta
if newCardHeight > minHeight && newCardHeight < maxHeight {
cardHeightConstraint.constant = newCardHeight
previousTranslationY = translation.y
}
if panGesture.state == UIGestureRecognizerState.ended {
previousTranslationY = 0
}
}
func handlePanOnCardAsSwipe(withVelocity velocity: CGFloat) {
if velocity.y > 0 {
dismissCard() // implementation not shown
} else {
maximizeCard() // implementation not shown
}
}下面是上述代码的实际应用演示。

https://stackoverflow.com/questions/4875255
复制相似问题