简体   繁体   中英

UILongPressGestureRecognizer not working

I want to detect UILongPressGestureRecognizer for the UIWebView tap- and hold ..such that When I long press for almost 3 seconds then the below if condition should be True then only
if (navigationType == UIWebViewNavigationTypeLinkClicked && longGesture ) but its not working....it continues in the loop for every time ..does not check for the longPressGesture time...

even I have Tried with the condition..

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

not working..where I am making mistake..

- (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;
  }

You should enable simultaneous gesture recognition because the UIWebView sets a few recognizers itself, yours are skipped : add this in your code

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

You're not setting the target-action for your gesture-recognizer, are you?

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

Setting a target-action will let you be notified, if the gesture fires or not! I'd start with that approach first an check if the "longPressGestureUpdated" method is being called.

Try out the following definition maybe:

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

(And enable simultaneous gesture recognizing as MilKyWaY advised already)

- (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];
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM