检测平移手势结束
我有一个视图,并将 UIPanGestureRecogniser 应用于该视图:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAnim:)];
[sliderView addGestureRecognizer:panGesture];
[panGesture release];
我可以很好地检测和处理手势。但是,我希望在手势结束后启动另一种方法。
我知道有两种方法可以进行这种检测。 touchesEnded
和 touchesCancelled
但是,我发现一旦触摸变成手势,即我移动手指足以保证,touchesCancelled
就会被调用手势调用和 touchesEnded
很少(如果有的话)被调用。
我希望能够向左/向右平移,然后在手势结束时启动另一个函数调用。我该怎么做?
I've got a view and I applied a UIPanGestureRecogniser to this view:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAnim:)];
[sliderView addGestureRecognizer:panGesture];
[panGesture release];
I can detect and process the gesture just fine. However, I wish to initiate another method once the gesture has ended.
I know there are two methods that allow this kind of detection. touchesEnded
and touchesCancelled
however, I've found that touchesCancelled
gets called as soon as the touch becomes a gesture i.e. I move my finger enough to warrant a gesture call and touchesEnded
rarely, if ever, gets called.
I want to be able to pan left / right and then initiate another function call upon gesture ending. How do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
可以通过使用
UIGestureRecognizerStateEnded
检查其状态来检测平移手势结束事件。检查下面的代码。
来自苹果文档
了解有关 UIPanGestureRecognizer 的更多信息
Pan gesture end event can be detected by checking its state with
UIGestureRecognizerStateEnded
.Check with the below code .
From Apple documentation
Read more about UIPanGestureRecognizer
可以通过使用
UIGestureRecognizerStateEnded
或UIGestureRecognizerStateCancelled
或UIGestureRecognizerStateFailed
检查其状态来检测平移手势结束事件,请使用以下代码进行检查。
Pan gesture end event can be detected by checking the it's state with
UIGestureRecognizerStateEnded
orUIGestureRecognizerStateCancelled
orUIGestureRecognizerStateFailed
Check with the below code .
上面的答案都是正确的,这只是 Swift 的更新答案。
斯威夫特3:
Above answers are all correct, this is just an updated one for Swift.
Swift 3:
在 Swift 4 中,使用 UIGestureRecognizerState.ending。
例如,
下面是视图控制器中使用手势对 UILabel 进行动画处理所需的所有代码,包括手势结束时的代码。
希望这有帮助。
In Swift 4, use UIGestureRecognizerState.ended.
e.g.
Below is all the code you need in a view controller to animate a UILabel with gesture, including when the gesture ends.
Hope this helps.
这对于两个(或更多)手指平底锅不起作用。在这种情况下,使用
numberOfMinimumTouches = 2
,平移将开始,您可以滚动/平移,但如果您抬起一根手指,平移仍将继续(移动到一根手指所在的位置,而不是在之间移动)两个手指),因为仅当所有手指抬起时才会发生State.ending
。要根据numberOfMinimumTouches
值停止,需要实现不同的方法。This would not work for a two (or more) finger pan. In this case with
numberOfMinimumTouches = 2
, the pan would start and you can scroll/pan around, but if you lift one finger, the pan will still continue (move to where the one finger is instead of between the two fingers), since theState.ended
is only when ALL fingers are lifted. To stop based on thenumberOfMinimumTouches
value, a different approach needs to be implemented.