我正在构建一个iPhone应用程序,让用户重新排列屏幕上的一些UI元素。
如何在同一个UIView中添加点击手势识别器和长按手势识别器?当我从长按中抬起手指时,轻敲手势识别器会触发。如何在用户执行长按时暂时禁用轻击手势识别器或阻止其触发?
谢谢!
答案 0 :(得分:53)
要允许两个手势一起使用,请实现以下委托方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
要使长按具有优先权,请执行以下操作:
[tapGesture requireGestureRecognizerToFail:longPress];
答案 1 :(得分:10)
要成功结合您需要:
1º添加到标题
的界面手势委托@interface ViewController : ViewController <UIGestureRecognizerDelegate>
2º创建手势事件并将视图添加到源文件中:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touch:)];
[tap setNumberOfTapsRequired:1]; // Set your own number here
[tap setDelegate:self]; // Add the <UIGestureRecognizerDelegate> protocol
UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTouch:)];
[longTap setNumberOfTapsRequired:0]; // Set your own number here
[longTap setMinimumPressDuration:1.0];
[longTap setDelegate:self]; // Add the <UIGestureRecognizerDelegate> protocol
[tap requireGestureRecognizerToFail:longTap]; // Priority long
[self.view addGestureRecognizer:tap];
[self.view addGestureRecognizer:longTap];
3º在源文件中添加回调:
- (void) touch: (UITapGestureRecognizer *)recognizer
{
CGPoint location = [recognizer locationInView: self.HUDview];
if (recognizer.state == UIGestureRecognizerStateBegan)
{
NSLog(@"touch UIGestureRecognizerStateBegan");
}
if (recognizer.state == UIGestureRecognizerStateEnded)
{
NSLog(@"touch UIGestureRecognizerStateEnded");
//NSLog(@"Position of touch: %.3f, %.3f", location.x, location.y); // Position landscape
}
}
- (void) longTouch: (UILongPressGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateBegan)
{
NSLog(@"longTouch UIGestureRecognizerStateBegan");
}
if (recognizer.state == UIGestureRecognizerStateEnded)
{
NSLog(@"longTouch UIGestureRecognizerStateEnded");
}
}
4º设置手势识别器:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
答案 2 :(得分:8)
作为替代方法,不要有两个单独的识别器 - 只需使用LongPress识别器进行这两个事件:
配置如下:
UILongPressGestureRecognizer* longPress = [ [ UILongPressGestureRecognizer alloc ] initWithTarget:self.nextResponder action:@selector(longPressEvent:)];
categoryPanelDrag.minimumPressDuration = 0.0;
然后处理如下:
- (BOOL)longPressEvent:(UILongPressGestureRecognizer *)gesture {
// _dragStarted is a class-level BOOL
if(UIGestureRecognizerStateBegan == gesture.state) {
_dragStarted = NO;
}
if(UIGestureRecognizerStateChanged == gesture.state) {
_dragStarted = YES;
// Do dragging stuff here
}
if(UIGestureRecognizerStateEnded == gesture.state) {
if (_dragStarted == NO)
{
// Do tap stuff here
}
else
{
// Do drag ended stuff here
}
}
return YES;
}
答案 3 :(得分:2)
我确实尝试了moby和熟练工的方法,但不知何故他们不适合我的项目,所以我解决了如下,
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
NSLog(@"%@ %ld",touch.description, touch.phase);
[self performSelector:@selector(checkTouch:) withObject:touch afterDelay:0.5];
return YES;
}
和
- (void)checkTouch:(UITouch *)touch{
NSLog(@"touch phase = %ld",touch.phase);
if (touch.phase == UITouchPhaseStationary) {
//still holding my hand and this means I wanted longPressTouch
}
if (touch.phase == UITouchPhaseEnded){
//I released my finger so it's obviously tap
}
}
它可能是更简单解决方案,但当然这取决于项目。
答案 4 :(得分:0)
你可以在代码中处理它,在长按期间,设置一个标志,如果在标志为真或其他任何时候调用tap,则不执行tap代码并重置标志。我不知道更好的方式