Tutorial :How to add a gesture recognizer to a UIWebView subclass?



Question:

If I add a gesture recognizer to a subclassed UIWebView instance called webView, e.g.:

UILongPressGestureRecognizer *_longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(webViewGestureRecognized:)];  _longPressRecognizer.allowableMovement = 20;  _longPressRecognizer.minimumPressDuration = 1.0f;  [webView addGestureRecognizer:_longPressRecognizer];  [_longPressRecognizer release], _longPressRecognizer = nil;   

The -webViewGestureRecognized: selector is not called when I perform a long press.

I have overridden the delegate method -gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: but the long-press selector is still not called.

- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {      return YES;  }  

Is there anything I can do to enable my own gesture recognizer on the web view?


Solution:1

as far as I know UIWebView shouldnt be subclassed as mentioned in Apple docs:

http://developer.apple.com/iphone/library/documentation/uikit/reference/UIWebView_Class/Reference/Reference.html


Solution:2

Just thought I would add the answer if anyone comes back to here. You have not assigned the delegate, therefore gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: is not called.

_longPressRecognizer.delegate = self;  

Do this and it works well.


Solution:3

You should use javascript in the UIWebView to detect the gestures. (You can then communicate that back to the Objective-C if you need to.) These are Apple's docs on detecting gestures and touches in Javascript. I also found this article helpful, although he uses a javascript library to deal with event binding, etc.

Here's a working example of pinching to zoom on a UIWebView that stands alone.

Note that it is the body element is listening for the event. On a short page it appears not to catch it if you do the event in the vast un-rendered whitespace below. (If anyone knows more about it, please leave a comment.)

<body id='body'>      Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco     <script type="text/javascript" charset="utf-8">        body = document.getElementById('body');        // (in percents)      zoom = 100;      maxZoom = 200;      minZoom = 50;      zoomIncrement = 25;        function gestureEnd(event) {          var newZoom;          if (event.scale > 1.0) {              // increase size              newZoom = zoom + zoomIncrement;          } else {              // decrease size              newZoom = zoom - zoomIncrement;          }            // don't exceed zoom boundaries          if (newZoom > maxZoom || newZoom < minZoom) {              return;          }          zoom = newZoom;          body.style.webkitTextSizeAdjust = zoom+"%";      }        body.addEventListener("gestureend", gestureEnd, false);        </script>  </body>  


Solution:4

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {  return YES;  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »