【问题标题】:Two UILongPressGestureRecognizer, one can fire, the other cannot两个UILongPressGestureRecognizer,一个可以触发,一个不能
【发布时间】:2012-03-15 11:35:09
【问题描述】:

伙计们,

我添加了两个 UILongPressGestureRecognizer。我想要的是当两个按钮被按下 0.3 秒时,启动“shortPressHandler”。如果用户继续按下这两个按钮 1.2 秒,则启动“longPressHandler”。现在我只启动了 shortPressHandler 并且从未启动过 longPressHandler。我认为这可能是因为首先识别了 shortPressGesture 而 longPressGesture 永远没有机会。谁能告诉我如何实现我想要的?提前致谢。

UILongPressGestureRecognizer *longPressGesture =[[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)] autorelease];
longPressGesture.numberOfTouchesRequired = 2;
longPressGesture.minimumPressDuration = 1.5;
longPressGesture.allowableMovement = 10;
longPressGesture.cancelsTouchesInView = NO;
longPressGesture.enabled = true;
[self.view addGestureRecognizer:longPressGesture];

UILongPressGestureRecognizer *shortPressGesture =[[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(shortPressHandler:)] autorelease];
shortPressGesture.numberOfTouchesRequired = 2;
shortPressGesture.minimumPressDuration = 0.3;
shortPressGesture.allowableMovement = 10;
shortPressGesture.cancelsTouchesInView = NO;
shortPressGesture.enabled = true;
[self.view addGestureRecognizer:shortPressGesture];

【问题讨论】:

  • 谢谢。如果我也想让 shortGesture 即使没有释放按钮也能触发,你知道该怎么做吗?
  • 对不起,我没有看到这条消息 - 如果你在你的帖子下写它,你必须用'@name ...'来解决它,如果我理解你的话你想要类似的东西:用户按下,shortPress 在 0.3 秒后被解雇,如果他不释放他的新闻, longPress 在 1.2 秒后被解雇。对吗?
  • @rokjarc 这正是我想要的。谢谢。
  • 这个有点棘手:一种快速而肮脏的方法是从 longPressHandler 调用 shortPressHandler。如果您需要在 0.3 秒按下后立即调用 shortPressHandler,那么您很可能必须转接UIGestureRecognizer
  • @rokjarc 非常感谢。您能否就我将覆盖哪个功能提供一些帮助?

标签: iphone ios cocoa ipad


【解决方案1】:

在添加 shortPressGesture 之前插入这一行:

 [shortPressGesture requireGestureRecognizerToFail:longPressGesture];

注意:shortGesture 不会在按住 0.3 秒后立即被调用,但如果点击时间在 0.3 秒到 1.2 秒之间,则会在松开点击时调用。如果点击时间超过 1.2 秒(您的代码中有 1.5 秒,这可能是错字),则只有 longPressGesture 会启动。

编辑:

但是,如果您希望您的事件处理程序都触发(在长按事件中),您应该这样做:

您的UIView 应该在.h 文件中实现<UIGestureRecognizerDelegate>

.m 文件中添加这个方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    return YES;
}

现在而不是添加该行:

[shortPressGesture requireGestureRecognizerToFail:longPressGesture];

你添加这两行:

shortPressGesture.delegate = self;
longPressGesture.delegate = self;

注意:如果您有任何其他UIGestureRecognisers 链接到您的UIVIew,则必须在shouldRecognizeSimultaneouslyWithGestureRecognizer: 中添加一些检查,否则您可以简单地返回YES

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 2013-12-03
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多