两个UILongPressGestureRecognizer,一个可以开火,另一个不可以

Glo*_*awk 6 iphone cocoa ipad ios

伙计们,

我添加了两个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];
Run Code Online (Sandbox Code Playgroud)

Rok*_*arc 6

在添加shortPressGesture之前插入此行:

 [shortPressGesture requireGestureRecognizerToFail:longPressGesture];
Run Code Online (Sandbox Code Playgroud)

注意:在持有0.3秒后不会立即调用shortGesture,但如果长度介于0.3秒和1.2秒之间则松开.如果点击时间超过1.2秒(你的代码中有1.5秒,这可能是一个错字),只有longPressGesture会启动.

编辑:

但是,如果您希望触发事件处理程序(如果长按),则应执行以下操作:

UIView应该实现 <UIGestureRecognizerDelegate>in .h文件.

.m文件中添加此方法:

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

    return YES;
}
Run Code Online (Sandbox Code Playgroud)

现在而不是添加行:

[shortPressGesture requireGestureRecognizerToFail:longPressGesture];
Run Code Online (Sandbox Code Playgroud)

你添加这两行:

shortPressGesture.delegate = self;
longPressGesture.delegate = self;
Run Code Online (Sandbox Code Playgroud)

注意:如果您有任何其他UIGestureRecognisers链接,您UIVIew将需要添加一些签入shouldRecognizeSimultaneouslyWithGestureRecognizer:,否则您只需返回YES.