UILongPressGestureRecognizer无法正常工作

时间:2013-02-04 12:03:48

标签: iphone ios uiwebview uigesturerecognizer

我想检测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;
  }

3 个答案:

答案 0 :(得分:3)

您应该启用同步手势识别,因为UIWebView会自行设置一些识别器,您的脚本将被跳过: 在您的代码中添加此内容

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

答案 1 :(得分: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建议的同时手势识别)

答案 2 :(得分: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];
  }
}