我在UIImageView中添加了4个手势识别器,单击,双击和固定手势都可以正常工作。但是,长按手势不起作用。这是为什么?
_imageView.userInteractionEnabled = YES ;
//single tap
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc ]initWithTarget:self action:@selector(singleTapAction:) ] ;
singleTap.numberOfTapsRequired = 1 ;
singleTap.numberOfTouchesRequired = 1 ;
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)] ;
doubleTap.numberOfTouchesRequired = 1 ;
doubleTap.numberOfTapsRequired = 2 ;
[singleTap requireGestureRecognizerToFail:doubleTap] ;
//pin gesture
UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)] ;
//long press gesture
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:) ] ;
[longPress requireGestureRecognizerToFail:singleTap ] ;
longPress.minimumPressDuration = 1 ;
longPress.numberOfTouchesRequired = 1 ;
longPress.numberOfTapsRequired = 1 ;
[_imageView addGestureRecognizer:longPress] ;
[_imageView addGestureRecognizer:pin] ;
[_imageView addGestureRecognizer:singleTap] ;
[_imageView addGestureRecognizer:doubleTap] ;
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
大家好,我添加了
shouldRecognizeSimultaneouslyWithGestureRecognizer
方法并设置了长按gesutre委托,但这仍然行不通。 最佳答案
您的错误可能在这里
[longPress requireGestureRecognizerToFail:longPress ] ;
为什么longPress要求自己失败?删除它。
您不了解requireGestureRecognizerToFail命令。它在需要其他手势无法触发时使用。如果长按不触发,请点按手势。
在您的情况下,夹取失败-> longPress失败->双击失败->单击
还删除此行:longPress.numberOfTapsRequired = 1;
注释掉您的代码并使用以下代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.imageView.userInteractionEnabled = YES;
self.imageView.multipleTouchEnabled = YES;
UIGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 1 ;
longPress.numberOfTouchesRequired = 1 ;
[longPress requireGestureRecognizerToFail:pinchGesture];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
doubleTap.numberOfTouchesRequired = 1;
doubleTap.numberOfTapsRequired = 2;
[doubleTap requireGestureRecognizerToFail:longPress];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
singleTap.numberOfTouchesRequired = 1;
singleTap.numberOfTapsRequired = 1;
[singleTap requireGestureRecognizerToFail:doubleTap];
[self.imageView addGestureRecognizer:pinchGesture];
[self.imageView addGestureRecognizer:longPress];
[self.imageView addGestureRecognizer:doubleTap];
[self.imageView addGestureRecognizer:singleTap];
}
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)gesture{
NSLog(@"Pinch");
}
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture{
NSLog(@"LongPress");
}
- (void)handleDoubleTap:(UITapGestureRecognizer *)gesture{
NSLog(@"Double Tap");
}
- (void)handleSingleTap:(UITapGestureRecognizer *)gesture{
NSLog(@"Single Tap");
}
关于ios - UILongPressGestureRecognizer无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20762513/