iosobjective-cuiwebviewuiswipegesturerecognizer

Swipe gesture for back/forward in UIWebView?


I have a WebView in my app.

Because it is a tabbed application I'm not able to add buttons for going back/forward on the website.

I want to go back/forward by swiping. Right swipe from the left side/edge is back… like in Safari browser for iOS.

How can I do it? I think i should use "Screen Edge Pan Gesture Recognizer", right?


Solution

  • Why not just use a swipe gesture recognizer?

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    
    // Setting the swipe direction.
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
    
    // Adding the swipe gesture on WebView
    [webView addGestureRecognizer:swipeLeft];
    [webView addGestureRecognizer:swipeRight];
    
    - (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {
    
    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"Left Swipe");
    }
    
    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"Right Swipe");   
    } 
    
    }