UIWEBVIEW 处理Touch事件
Recently, I was working on a project which required detection of tap and events on the UIWebView. We wanted to find out the HTML element on which the user taps in the UIWebView and then depending on the element tapped some action was to be performed. After some Googling, I found out the most of the users lay a transparent UIView on top of the UIWebView, re-implement the touch methods of UIResponder class (Ex: -touchesBegan:withEvent:) and then pass the events to the UIWebView. This method is explained in detail?here. There are multiple problems with the method.
We ultimately found out that the right way to implement this is by sub-classing UIWindow and re-implementing the -sendEvent: method. Here is how you can do it. First, create a UIWindow sub-class
@interface WebViewController : UIViewController<TapDetectingWindowDelegate> { IBOutlet UIWebView *mHtmlViewer; TapDetectingWindow *mWindow;}- (void)viewDidLoad { [super viewDidLoad]; mWindow = (TapDetectingWindow *)[[UIApplication sharedApplication].windows objectAtIndex:0]; mWindow.viewToObserve = mHtmlViewer; mWindow.controllerThatObserves = self;}
?Remember you'll need to write the method userDidTapWebView in your controller class. This is the method that is called in order to send the event information to the controller class. In our case above we are sending the point in the UIWebView at which the user tapped. Hope this was useful. Please let me know your suggestions and feedback.
?
转自:http://mithin.in/2009/08/26/detecting-taps-and-events-on-uiwebview-the-right-way/