3

我想检测UILongPressGestureRecognizer轻按UIWebView并按住..这样当我长按近 3 秒时,下面的if条件应该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;
  }
4

3 回答 3

3

您应该启用同时手势识别,因为 UIWebView 自己设置了一些识别器,您的被跳过:在您的代码中添加这个

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
于 2013-02-04T12:14:56.507 回答
2

您没有为手势识别器设置目标动作,是吗?

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 的建议启用同时手势识别)

于 2013-02-04T12:48:43.543 回答
2
- (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];
  }
}
于 2013-02-05T06:49:18.403 回答