Skip to main content

Does UIGestureRecognizer work on a UIWebView?


I am attempting to get a UIGestureRecognizer working with a UIWebview which is a subview of a UIScrollView. This sounds odd but when I have the numberOfTouchesRequired set to 2 the selector fires, but when numberOfTouchesRequired is set to one the selector doesn't fire.



Here is my code:




UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(select1:)];
tap1.numberOfTouchesRequired = 2;
tap1.numberOfTapsRequired = 1;
tap1.delegate = self;
[self.ans1WebView addGestureRecognizer:tap1];
[tap1 release];

- (void) select1:(UILongPressGestureRecognizer *)sender {
//Do Stuff
}



I've confirmed this by using the Apple sample for UIGestureRecognizer and inserting a webview in their nib. Their tap code works everywhere but inside the area of the webview.


Source: Tips4allCCNA FINAL EXAM

Comments

  1. From what I have seen, UIWebView does not play well with others. For gesture recognizers, you could try returning YES from:

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


    I had to be backwards compatible with 3.0 so I ended up doing all the gesture handling with Javascript inside the UIWebView. Not a good solution, but it worked for my needs. I was able to capture a long press on an element, as well as tap, swipe, pinch and rotate. Of course, I was using only local content.

    Edit:

    You can look in PhoneGap for an example of sending messages to the delegate of the UIWebView. In a nutshell, you use document.location = "myscheme:mycommand?myarguments" then parse out the command and arguments from this delegate callback:

    -(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
    if ( [[[inRequest URL] absoluteString] hasPrefix:@"myscheme:"] ) {
    //.. parse arguments
    return NO;
    }
    }


    You can see in the PhoneGap code that they set up a queue and timer to send the messages. Depending on your usage, you may need to do the same thing. There are other questions on SO on this topic.

    Here is the Event Handling documentation. In my case I added event listeners to document from <body onload="myloader();">

    function myloader() {
    document.addEventListener( 'touchcancel' , touch_cancel , false );
    document.addEventListener( 'touchstart' , touch_start , false );
    document.addEventListener( 'touchmove' , touch_move , false );
    document.addEventListener( 'touchend' , touch_end , false );
    };


    The actual event handling depends a lot on your needs. Each event handler will receive a TouchEvent with a touches property where each item is a Touch. You can record the start time and location in your touchstart handler. If the touches move to far or the wrong amount of time passes it is not a long touch.

    WebKit may try to handle a long touch to start a selection and copy. In your event handler you can use event.preventDefault(); to stop the default behavior. I also found the -webkit-user-select:none css property handy for some things.

    var touch = {};
    function touch_start( event /TouchEvent/ {
    event.preventDefault();
    touch = {};
    touch.startX = event.touches.item(0).pageX;
    touch.startY = event.touches.item(0).pageY;
    touch.startT = ( new Date() ).getTime();
    }

    This is only the second project I have used javascript with. You can find better examples elsewhere.

    As you can see this is no quick answer to your problem. I am pretty happy with the results I got. The html I was working with had no interactive elements beyond a link or two.

    ReplyDelete
  2. UIWebView has its own private views, which also has gesture recognizers attached. Hence, precedence rules keep any gesture recognizers added to a UIWebView from working properly.

    One option is to implement the UIGestureRecognizerDelegate protocol and implement the method gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer. Return YES from this method for other tap gestures. This way you'll get your tap handler called, and the web view will still get its called.

    See this on the Apple dev forums.

    ReplyDelete
  3. You may also find my answer here helpful. It's a working example of handling the pinch gesture in UIWebView's javascript.

    (You could communicate the events back to the Objective-C if you wanted to. See my answer here.)

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.