This is something that had me  banging my head against the wall and thought I would share. The use case is using the left and right swipe gesture to register against the UIWebView/UIView that then calls a swipe function. For my case I was using it to change navigation on a UITableView, but you could use is for back/forward on the UIWebView itself or use the same process to register custom gestures. Of all the resources I found, they use a UIView over the UIWebView, or a disable interactivity on the UIWebView and then push touch event into it. The code is used inside a UIViewController with a UIWebVIew present.

- (void)viewDidLoad
{
     [super viewDidLoad];
     UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self  action:@selector(swipeRightAction:)];
     swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
     swipeRight.delegate = self;
     [webView addGestureRecognizer:swipeRight];
 
     UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)];
     swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
     swipeLeft.delegate = self;
     [webView addGestureRecognizer:swipeLeft];
}
 
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
     return YES;
}
 
- (void)swipeRightAction:(id)ignored
{
     NSLog(@"Swipe Right");
     //add Function
}
 
- (void)swipeLeftAction:(id)ignored
{
     NSLog(@"Swipe Left");
     //add Function
}

6 thoughts on “Swipe Gesture with UIWebView

  1. Works nicely. Thanks too.
    Two points: 1. I had to add into the viewcontroller header file
    2. The webView appears to lose its usual gesture recognition behaviour – such as double tap to zoom, or pinch to zoom in and out. Any thoughts on reinstating these without reinventing the wheel, so to speak?

    Ian

  2. Thanks Thanks Thanks a lot Justin! I’ve been banging my head for hours to make this work using touches methods and your code worked perfectly in just seconds. You saved my time!

    I really appreciate it! :)

    Thanks again.

Leave a reply