3

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;
  }
Christien
  • 1,213
  • 4
  • 18
  • 32

3 Answers3

3

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;
}
DD_
  • 7,230
  • 11
  • 38
  • 59
  • means if i use `longGesture.minimumPressDuration = 3` ; and `if (navigationType == UIWebViewNavigationTypeLinkClicked && longGesture.minimumPressDuration)` it makes the if condition TRUE without holding for 3 seconds – Christien Feb 04 '13 at 12:39
  • @MilKyWaY - I had an issue months ago where I struggled to get a LongTouch working with a normal touch recognizer - and ended up giving up and not using the longtouch because I thought doing both was impossible. And then I see this. Thank you very much. – James Boutcher Feb 04 '13 at 12:39
  • @JamesBoutcher is it working for you ?? bcd its not working for me too .. where i am getting wrong – Christien Feb 04 '13 at 12:41
  • @Christien - I don't have your exact problem (dealing w/ gesturerecognizers in a UIWebView). Maybe look at this? http://stackoverflow.com/questions/5504955/custom-control-overlay-on-uiwebview-on-single-tap-touch-in-iphone – James Boutcher Feb 04 '13 at 12:47
  • work for me, bcoz I added some more gestures recognizer in same view. – gauravds Sep 16 '14 at 16:45
2

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)

Nenad M
  • 3,055
  • 20
  • 26
  • hey listen...target-action is been called according to the minimumPressDuration which is been set by me(3seconds)...now problem is in `IF condition` `if (navigationType == UIWebViewNavigationTypeLinkClicked && longGesture )` it should be TRUE after 3 sec ..but it does not check the longGesture time – Christien Feb 04 '13 at 12:57
  • Nenad is right, if you don't set an action to the longPressGesture object, the action will NEVER happen. You should read the complete Apple documentation here (about gesture) : http://developer.apple.com/library/ios/#documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html – Ashbay Feb 04 '13 at 16:13
  • Is it possible for you to hook up your if-stmt. logic inside the 'longPressGestureUpdated' action method? – Nenad M Feb 04 '13 at 16:32
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];
  }
}
Paresh Karnawat
  • 312
  • 1
  • 3
  • 13
  • ya thanks man! but that I wanted `UILongPressGestureRecognizer` should be done only on some `link` means with condition `if (navigationType == UIWebViewNavigationTypeLinkClicked)` ..i want `UILongPressGestureRecognizer` – Christien Feb 05 '13 at 07:08
  • lik this if (navigationType == UIWebViewNavigationTypeLinkClicked && longGesture.minimumPressDuration > 3 )` means if it is link and on that link I tapped for 3 seconds ...then uiactionsheet would display – Christien Feb 05 '13 at 07:10