人,
我添加了两个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];
答案 0 :(得分:6)
在添加shortPressGesture之前插入此行:
[shortPressGesture requireGestureRecognizerToFail:longPressGesture];
注意:在持有0.3秒后不会立即调用shortGesture,但如果长度介于0.3秒和1.2秒之间,则释放水龙头。如果点击时间超过1.2秒(你的代码中有1.5秒,这可能是一个错字)只有longPressGesture会启动。
编辑:
但是,如果你希望你的事件处理程序都能解雇(如果长按),你应该这样做:
您的UIView
应该在<UIGestureRecognizerDelegate>
文件中实施.h
。
在.m
文件中添加此方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
现在代替添加以下行:
[shortPressGesture requireGestureRecognizerToFail:longPressGesture];
您添加以下两行:
shortPressGesture.delegate = self;
longPressGesture.delegate = self;
注意:如果您的UIGestureRecognisers
链接了任何其他UIVIew
,则必须在shouldRecognizeSimultaneouslyWithGestureRecognizer:
中添加一些检查,否则您只需返回YES
。