简体   繁体   English

如何以编程方式结束/重置UIGestureRecognizer?

[英]How do I programmatically end/reset a UIGestureRecognizer?

Say I am currently tracking a drag gesture. 假设我正在跟踪拖动手势。 In my event handler I use a threshold to determine when the drag results in an action. 在我的事件处理程序中,我使用阈值来确定拖动何时导致操作。 When the threshold is crossed, I want to indicate that the drag gesture has completed. 超过阈值时,我想指示拖动手势已完成。

The only thing I can find in the docs is this line here : 我可以在文档中找到的唯一的事情就是这条线在这里

If you change this property to NO while a gesture recognizer is currently recognizing a gesture, the gesture recognizer transitions to a cancelled state. 如果在手势识别器当前正在识别手势时将此属性更改为“否”,则手势识别器将转换为已取消状态。

So: 所以:

if (translation.y > 100) {
    // do action
    [self doAction];

    //end recognizer
    sender.enabled = NO;
    sender.enabled = YES;
}

This works but it looks like there might be a neater way. 这有效,但看起来可能有一个更简洁的方式。

Does anyone know of another way to indicate that a gesture has ended programmatically? 有没有人知道另一种方式来表明手势已经以编程方式结束? I would expect something like a method -end: that generates a final event with state UIGestureRecognizerStateEnded . 我希望类似方法-end:生成一个状态为UIGestureRecognizerStateEnded的最终事件。

Have you defined a custom UIGestureRecognizer? 您是否定义了自定义UIGestureRecognizer? If the gesture you're recognizing is different from the standard ones defined by Apple because it has a different threshold or is otherwise not the same as a regular UIPanGestureRecognizer, then it might make sense to create your own UIGestureRecognizer. 如果您识别的手势与Apple定义的手势不同,因为它具有不同的阈值或者与常规UIPanGestureRecognizer不同,那么创建自己的UIGestureRecognizer可能是有意义的。 ( see subclassing notes ) 参见子类注释

If you have subclassed UIGestureRecognizer, you can simply set the state like this: 如果你有UIGestureRecognizer的子类,你可以简单地设置这样的状态:

  self.state = UIGestureRecognizerStateEnded;

You probably want to do this in the touchesMoved:withEvent: method. 您可能希望在touchesMoved:withEvent:方法中执行此操作。 Also note: 另请注意:

"Subclasses of UIGestureRecognizer must import UIGestureRecognizerSubclass.h. This header file contains a redeclaration of state that makes it read-write." “UIGestureRecognizer的子类必须导入UIGestureRecognizerSubclass.h。此头文件包含重新声明的状态,使其成为读写。”

On the other hand, if you're only implementing a UIGestureRecognizerDelegate, the state is read-only, and there is no way to directly set it. 另一方面,如果您只实现了UIGestureRecognizerDelegate,则状态是只读的,并且无法直接设置它。 In that case your method of disabling/enabling might be the best you can do. 在这种情况下,您的禁用/启用方法可能是您可以做的最好的方法。

With the code you showed you need to have the logic for starting the animation when the gesture recognizer is canceled and I'd say this is not good as there are other ways that this gesture recognizer can be canceled without you wanting to have the animation done. 使用您展示的代码,您需要具有在取消手势识别器时启动动画的逻辑,并且我说这不是很好,因为有其他方法可以取消此手势识别器而无需您想要完成动画。

Considering you have a method for starting the animation you just need to call this method when the threshold is passed and when the gesture ends normally. 考虑到你有一个启动动画的方法,你只需要在传递阈值和手势正常结束时调用此方法。 Two different occasions then. 然后两个不同的场合。 The code you presented would look like this: 您提供的代码如下所示:

if (translation.y > 100) {
    // do action
    [self finishFlip];
    sender.enabled = NO;
    sender.enabled = YES;
}

Canceling the gesture here may be useful also if that prevents any following actions if the user keeps dragging its finger. 如果用户继续拖动其手指,则在此处取消手势可能也是有用的。

If you'll have a team developing over this and you need a specific event to happen you should subclass the gesture recognizer as nont suggested. 如果你有一个团队开发这个并且你需要一个特定的事件发生,你应该将手势识别器子类化为没有建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM