如何知道捏合手势何时完成(UIGestureRecognizer)

发布于 2024-09-10 08:16:33 字数 118 浏览 4 评论 0原文

我想在我的 UIPinchGestureRecognizer 完成捏合手势时收到回调。此外,如果知道完成的手势是放大还是缩小,那就太好了。

有谁知道使用方法吗?或者采取什么方法去做?

谢谢!

I want to get a callback when my UIPinchGestureRecognizer finished a pinch-gesture. Moreover it would be great to know if the finished gesture was a zoom in or a zoom out.

Does anyone know a method to use? Or the approach to do?

Thanks!

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

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

发布评论

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

评论(4

余生再见 2024-09-17 08:16:33

另一种方法是,您可以只在目标处理程序方法中检查手势识别器的状态,而不是重写 TouchsEnded:。

  -(void)handlePinchGesture:(UIGestureRecognizer*)gestureRecognizer {    
    if(UIGestureRecognizerStateEnded == [gestureRecognizer state]){
      // do something
    }
  }

Another approach instead of overriding touchesEnded:, is that you could just check the state of the gesture recognizer in your target handler method.

  -(void)handlePinchGesture:(UIGestureRecognizer*)gestureRecognizer {    
    if(UIGestureRecognizerStateEnded == [gestureRecognizer state]){
      // do something
    }
  }
朱染 2024-09-17 08:16:33

您可以通过UIPinchGestureRecognizer的scale属性知道它是放大还是缩小。

只需覆盖它的 TouchsEnded: 方法即可获取回调(如果您愿意,还可以调用其他方法)。

You can know if it was a zoom in or out by the scale property of the UIPinchGestureRecognizer.

Just overrride it's touchesEnded: method to get a callback (and the call some other method if you wish).

七色彩虹 2024-09-17 08:16:33

不需要子类化的最佳方法是检查动作处理程序中手势识别实例的“状态”属性。状态将在手势生命周期的所有阶段发生变化。您正在寻找的状态更改是 UIGestureRecognizerStateEnded。检查 UIGestureRecognizerStateCancelled 也是一个很好的做法。

The best approach which does not require subclassing is to examine the "state" property on the gesture recognized instance in your action handler. The state will change during all phases of the lifecycle of the gesture. The state change you're looking for is UIGestureRecognizerStateEnded. It is also good practice to check for UIGestureRecognizerStateCancelled as well.

无名指的心愿 2024-09-17 08:16:33

由于某种原因,在我的设备上进行测试时,我并未始终收到 .end 的捏合手势识别器状态。我已经设法让它偶尔触发一次,但它非常不一致。

(我想知道是否是因为我在 UICollectionView 上附加了一个捏合手势识别器,但我还没有尝试用其他视图来确认。因此,我不认为重写 touchesEnded 是我可以考虑的解决方案,因为正在使用其他触摸事件/手势。)

我可以执行的最一致的检查是确认手势识别器上的触摸次数

假设您已将捏合手势识别器附加到视图:

let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(didReceivePinchGesture))
yourView.addGestureRecognizer(pinchGesture)

在处理程序内,我检查触摸次数以确保它是捏合手势。如果不是两个,那么我可以推断这是手势的结束:

@objc func didReceivePinchGesture(_ gestureRecognizer: UIPinchGestureRecognizer) {
    guard gestureRecognizer.numberOfTouches == 2 else {
        // Logic for pinch gesture ended
        return
    }

    if gestureRecognizer.state == .began {
        ...
    } else if gestureRecognizer.state == .changed {
        ...
    } else {
        // Same logic for pinch gesture ended (though rarely invoked)
    }
}

警告: 测试时,类似于 .ending 状态不会被触发如果您没有在完全相同的毫秒内抬起手指,那么即使您的手指没有在完全相同的时间触摸屏幕,即使一开始只有一次触摸,手势识别器也会被调用。

当状态等于 .began 时,我有明确的逻辑开始处理手势,因此这对我的逻辑没有任何负面影响。然而,根据您想要实现的目标,这可能是您所关心的问题。

For some reason, I am not consistently receiving a pinch gesture recognizer state of .ended when testing on my devices. I've managed to get this to trigger once in awhile, but it is wildly inconsistent.

(I wonder if it is because I have a pinch gesture recognizer attached to a UICollectionView, but I haven't tried it with other views to confirm. For this reason, I don't think overriding touchesEnded is a solution that I can consider since other touch events/gestures are being used.)

The most consistent check I could perform was to confirm the number of touches on the gesture recognizer.

Assuming you have attached a pinch gesture recognizer to a view:

let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(didReceivePinchGesture))
yourView.addGestureRecognizer(pinchGesture)

Inside the handler, I check the number of touches to ensure that it is a pinch gesture. If it is not two, then I can deduce that this is the end of the gesture:

@objc func didReceivePinchGesture(_ gestureRecognizer: UIPinchGestureRecognizer) {
    guard gestureRecognizer.numberOfTouches == 2 else {
        // Logic for pinch gesture ended
        return
    }

    if gestureRecognizer.state == .began {
        ...
    } else if gestureRecognizer.state == .changed {
        ...
    } else {
        // Same logic for pinch gesture ended (though rarely invoked)
    }
}

Caveat: When testing this, similar to how the .ended state does not get triggered if you don't lift your fingers at exactly the same millisecond, it appears as though the gesture recognizer is called even when there's only one touch at the beginning if your fingers do not touch the screen at the exact same time.

I have explicit logic to begin processing the gesture when the state equals .began so this didn't have any negative impacts on my logic. However, depending on what you are trying to accomplish this may be a concern for you.

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