Krunal
Krunal

Reputation: 6490

detect Swipe gesture in UIWebview

I am new to iPhone developer,

I made epub reader and loaded each page of epub in my webview

What i want to is, when user does right swipe gesture 2nd time then i want to navigate to new page, i do not want to do anything when user does right swipe gesture for first time.

UISwipeGestureRecognizer *swipeRight

Is there any method something like,

if(swipeRight.touch.count > 2)
{
  // do this
}

Any help will be appriciated.

Thanks In Advance !

EDIT

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    float scrollViewHeight = scrollView.frame.size.height;
    float scrollContentSizeHeight = scrollView.contentSize.height;
    float scrollOffset = scrollView.contentOffset.y;

    if (scrollOffset == 0)
    {
        swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self  action:@selector(swipeLeftAction:)];
        swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
        swipeUp.numberOfTouchesRequired=2;
        swipeUp.delegate = (id<UIGestureRecognizerDelegate>)self;
        swipeUp.cancelsTouchesInView=YES;
        [_webview addGestureRecognizer:swipeUp];  
    }
    else if (scrollOffset + scrollViewHeight == scrollContentSizeHeight)
    {
        swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self  action:@selector(swipeRightAction:)];
        swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
        swipeDown.numberOfTouchesRequired=2;
        swipeDown.delegate = (id<UIGestureRecognizerDelegate>)self;
        swipeDown.cancelsTouchesInView=YES;
        [_webview addGestureRecognizer:swipeDown];

    }

Upvotes: 3

Views: 5248

Answers (4)

Johannes Fahrenkrug
Johannes Fahrenkrug

Reputation: 44700

You can tell the UIWebView's UIScrollView that its UIPanGestureRecognizer should only fire when your own UISwipeGestureRecognizer has failed.

This is how you do it:

UISwipeGestureRecognizer *rightSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
UISwipeGestureRecognizer *leftSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
leftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:rightSwipeGesture];
[self.view addGestureRecognizer:leftSwipeGesture];

[_webView.scrollView.panGestureRecognizer requireGestureRecognizerToFail:rightSwipeGesture];
[_webView.scrollView.panGestureRecognizer requireGestureRecognizerToFail:leftSwipeGesture];

That should do the trick for you.

Upvotes: 6

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

Just attach UIGestureRecognizer subclass to that view and hold on for calls...

UISwipeGestureRecognizer* rightSwipeRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(someAction)];
rightSwipeRecognizer.numberOfTouchesRequired = 2;
rightSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
rightSwipeRecognizer.cancelsTouchesInView = YES;
[self.webView addGestureRecognizer:rightSwipeRecognizer]; // add in your webviewrightSwipeRecognizer

Upvotes: 4

sergio
sergio

Reputation: 69027

I don't think that swipe gestures offer support for the kind of behavior you are aiming at, but you can easily accomplish it by doing the following:

  1. on the first swipe, set a flag and start a timer; for the rest do nothing;

  2. on the second swipe,

    a. if the timer has fired (when firing, the timer reset the flag), do as per point 1.

    b. is the timer has not fired (the flag is still set), then do you action and cancel the timer.

You might event think of defining a subclass of UISwipeGestureRecognizer to encapsulate all this behavior.

Upvotes: 1

Narayana Rao Routhu
Narayana Rao Routhu

Reputation: 6323

Try like below it will help you
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
    rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    rightRecognizer.numberOfTouchesRequired = 2;
    [self.view addGestureRecognizer:rightRecognizer];
    [rightRecognizer release];

Upvotes: 1

Related Questions