Search code examples
iphoneipaduiwebviewuigesturerecognizertouchesmoved

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

    }

Solution

  • 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