Search code examples
objective-ciosuiswipegesturerecognizer

How to handle SwipeGestures on top of a UIWebView in the ViewController?


I am diving into iOS development and for that I am developing a simple app that displays blogposts.

I have the following setup in my DetailViewController:

My DetailViewController

What I want to implement is that when a user swipes left or right on the screen, I change the content of the webView.

The swipe-gestures however seem to be swallowed by the UIWebView, so I figured I could make a transparent UIView ("Posts Detail Touch Interceptor View") on top and have it handle the swipe-gestures and forward them to the DetailViewController.

This works, the swipe-handlers in DetailViewController get called, but I can no longer scroll in the webView or press any buttons. I assume, because the transparent UIView consumes all events before they reach the UIWebView.

What is the best way to make all controls below the transparent view respond to events normally, while the swipe gestures get forwarded to the DetailViewController?


Solution

  • You could still add the swipe recognizer to the UIWebView and implement this method in your gesture recognizer delegate:

        - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    

    Alternatively, you can see this SO post to handle the event in both views:

    Touch event handled by multiple views

    I recommend first trying to use your own gesture recognizer in the UIWebView. I have done this before successfully.