如何知道捏合手势何时完成(UIGestureRecognizer)
我想在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
另一种方法是,您可以只在目标处理程序方法中检查手势识别器的状态,而不是重写 TouchsEnded:。
Another approach instead of overriding touchesEnded:, is that you could just check the state of the gesture recognizer in your target handler method.
您可以通过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).
不需要子类化的最佳方法是检查动作处理程序中手势识别实例的“状态”属性。状态将在手势生命周期的所有阶段发生变化。您正在寻找的状态更改是 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.
由于某种原因,在我的设备上进行测试时,我并未始终收到
.end
的捏合手势识别器状态。我已经设法让它偶尔触发一次,但它非常不一致。(我想知道是否是因为我在
UICollectionView
上附加了一个捏合手势识别器,但我还没有尝试用其他视图来确认。因此,我不认为重写touchesEnded
是我可以考虑的解决方案,因为正在使用其他触摸事件/手势。)我可以执行的最一致的检查是确认手势识别器上的触摸次数。
假设您已将捏合手势识别器附加到视图:
在处理程序内,我检查触摸次数以确保它是捏合手势。如果不是两个,那么我可以推断这是手势的结束:
警告: 测试时,类似于
.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 overridingtouchesEnded
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:
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:
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.