UILongPressGestureRecognizer不起作用

我想检测UILongPressGestureRecognizerUIWebView并按住..如果我长按近3秒,那么下面的条件应该是True然后只
if (navigationType == UIWebViewNavigationTypeLinkClicked && longGesture )但它不工作….它继续在循环中每次..不检查longPressGesture时间…

即使我已经试过条件..

if (navigationType == UIWebViewNavigationTypeLinkClicked && longGesture.minimumPressDuration> 3 )

不工作..我在犯错误的地方..

 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] init]; longGesture.numberOfTapsRequired = 1; longGesture.numberOfTouchesRequired = 1; longGesture.minimumPressDuration = 3 ; longGesture.delegate = self; // longGesture.allowableMovement = 50; [self.webView addGestureRecognizer:longGesture]; if (navigationType == UIWebViewNavigationTypeLinkClicked && longGesture ) { // Call your custom actionsheet and use the requestURL to do what you want :) UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@" OPTIONS " delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; [sheet addButtonWithTitle:@"Open"]; [sheet addButtonWithTitle:@"Copy"]; // Set cancel button index to the one we just added so that we know which one it is in delegate call // NB - This also causes this button to be shown with a black background sheet.cancelButtonIndex = sheet.numberOfButtons-1; [sheet showInView:webView]; return NO; } 

您应该启用同时手势识别,因为UIWebView本身设置了一些识别器,您的手机被跳过:添加此代码

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } 
 - (void)viewDidLoad { [super viewDidLoad]; UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)]; longPress.numberOfTouchesRequired = 1; [self.view addGestureRecognizer:longPress]; } -(void)handleLongPress:(UILongPressGestureRecognizer *)gesture { if (gesture.state == UIGestureRecognizerStateBegan) { UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@" OPTIONS " delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; [sheet addButtonWithTitle:@"Open"]; [sheet addButtonWithTitle:@"Copy"]; // Set cancel button index to the one we just added so that we know which one it is in delegate call // NB - This also causes this button to be shown with a black background sheet.cancelButtonIndex = sheet.numberOfButtons-1; [sheet showInView:webView]; } } 

你没有设置你的手势识别器的目标行动,是吗?

 UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureUpdated:)]; 

设置一个目标行动将让你得到通知,如果手势发生或没有! 我将首先检查是否正在调用“longPressGestureUpdated”方法。

也许尝试以下定义:

  longPGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureUpdated:)]; longPressGesture.numberOfTouchesRequired = 1; longPressGesture.delegate = self; longPressGesture.cancelsTouchesInView = NO; 

(并且已经启用了MilKyWaY的build议,同时启用手势识别)