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)
在添加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
.
归档时间: |
|
查看次数: |
1452 次 |
最近记录: |