手势方法(平移手势和滑动手势)之间是否有优先级条件?

发布于 2024-10-15 16:27:37 字数 104 浏览 4 评论 0原文

我正在开发一个应用程序,其中使用了平移手势和滑动手势。因此,每次我执行“滑动手势”时,但总是会调用“平移手势”中的方法,而不会调用“滑动手势”方法。

所有手势方法之间有优先级吗?

I am developing an application where I have used the Pan Gesture as well as Swipe Gesture. So every time I do the Swipe Gesture but the method from the Pan Gesture is always getting called and Swipe Gesture method is not getting called.

Is there any priority between all the gesture method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滥情哥ㄟ 2024-10-22 16:27:37

您可以通过实现 UIGestureRecognizerDelegate 协议的以下方法来并行调用它们:

- (BOOL)gestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer 
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UISwipeGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

You can call them in parallel by implementing the following method of the UIGestureRecognizerDelegate protocol:

- (BOOL)gestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer 
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UISwipeGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
演多会厌 2024-10-22 16:27:37

UIGestureRecognizer 类上有一个名为“cancelsTouchesInView”的属性,默认为 YES。这将导致所有待处理的手势被取消。平移手势首先被识别,因为它不需要“触摸”事件,因此它取消了滑动手势。

如果您希望两种手势都能被识别,请尝试添加:

[yourPanGestureInstance setCancelsTouchesInView:NO];

There is a property on the UIGestureRecognizer class called "cancelsTouchesInView" which defaults to YES. This will cause any pending gestures to be canceled. The Pan gesture is getting recognized first since it does not need to have a "touch up" event, so it cancels the Swipe gesture.

If you want both gestures to be recognized, try adding:

[yourPanGestureInstance setCancelsTouchesInView:NO];

蓝眸 2024-10-22 16:27:37

优先滑动

您可以使用 require(toFail:) 方法优先考虑 UIGestureRecognizer

@IBOutlet var myPanGestureRecognizer: UIPanGestureRecognizer!
@IBOutlet var mySwipeGestureRecognizer: UISwipeGestureRecognizer!

myPanGesture.require(toFail: mySwipeGestureRecognizer)

现在,仅当您的滑动失败时,您的平移才会执行。


对所有内容都使用平移

如果滑动平移手势识别器在此设置中不能很好地发挥作用,您可以将所有逻辑滚动到平移手势识别器中以获得更多控制。

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

这是上述代码的实际演示。

输入图片此处描述

Give priority to swipe

You can give priority to a UIGestureRecognizer with the require(toFail:) method.

@IBOutlet var myPanGestureRecognizer: UIPanGestureRecognizer!
@IBOutlet var mySwipeGestureRecognizer: UISwipeGestureRecognizer!

myPanGesture.require(toFail: mySwipeGestureRecognizer)

Now your pan will only execute if your swipe fails.


Use pan for everything

If the swipe and pan gesture recognizers don't play nicely with this setup, you can roll all of your logic into the pan gesture recognizer for more control.

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

Here's a demo of the above code in action.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文