iPhone Development

Just another iPhone Blog

How to pass touch events to UIWebView

I was wondering how to pass touch events to UIWebview and here it is

UIWebview does not handle the touch event, instead it is some other view (i.e UIWebDocumentView) which handles the events. But, many people wonder, including me, how this undocumented, private object come into picture.

And the answer is UIWebview seems to override the hitTest:withEvent: method which on invoked returns the UIWebDocumentView, which means that all the subsequent touch events will be routed to UIWebDocumentView

Now how do we by pass this situation, so that one of our view class should get control over the touch event?

This can be done by subclassing the UIWebview and overriding the hitTest:withEvent method and invoking super class method

Code:

-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event {

_responderView = [super hitTest:point withEvent:event];//capture the view which actually responds to the touch events

return self;//pass self so that the touchesBegan,touchesMoved and other events will be routed to this class itself

}

 

//override touch events

 

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

[ _responderView touchesBegan:touches withEvent:event];

}


Hope this helps

Same topic is also discussed here

FYI, How to pass touch events to UIWebview

Advertisements

2 responses to “How to pass touch events to UIWebView

  1. david June 27, 2010 at 11:26 pm

    Should be

    -(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent*)event

    • bharath2020 July 10, 2010 at 5:32 pm

      Thanks Post Updated

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: