I have a program that needs to check whether UIWebView has touch actions. Unfortunately, touchesBegan and other events on UIWebView cannot be detected. I checked on the Internet and found many solutions, such as adding a transparent UIView to UIWebView, resetting the sendEvent of UIWindow, or resetting the hitest method of UIWebView. either, the solution is not perfect, either it is too complicated. After the experiment, the method I used is the simplest (or I don't have time to write it here). Of course, I only need to check that there is a touch action on UIWebView. My method is to use UITapGestureRecognizer. There are two key points. See my code (my code is in a UIViewController ):
[Cpp] UITapGestureRecognizer * singleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (handleSingleTap :)];
[Self. view addGestureRecognizer: singleTap];
SingleTap. delegate = self;
SingleTap. cancelsTouchesInView = NO;
[SingleTap release];
UITapGestureRecognizer * singleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (handleSingleTap :)];
[Self. view addGestureRecognizer: singleTap];
SingleTap. delegate = self;
SingleTap. cancelsTouchesInView = NO;
[SingleTap release];
The third line is the first key, that is, the delegate of UITapGuestureRecognizer must be set (generally we directly use initWithTarget without setting its delegate ).
The second key is to return YES in the following delegate method. Www.2cto.com
[Cpp]-(BOOL) gestureRecognizer :( UIGestureRecognizer *) gestureRecognizer plugin :( UIGestureRecognizer *) otherGestureRecognizer
{
Return YES;
}
-(BOOL) gestureRecognizer :( UIGestureRecognizer *) gestureRecognizer author :( UIGestureRecognizer *) otherGestureRecognizer
{
Return YES;
}
By doing this, your UIWebView can respond to the touch event. You can use different UIGestureRecognizer to meet your different requirements. (Complete the handleSingleTap Method on your own ).
From the column zcl316369