开发者

Using a UIGestureRecognizer in a UIWebView

Previously I've used the tap Detecting Window technique to detect taps in a UIWebView, but now I've tried to use gesture recognizers instead. The following code is in the viewDidLoad method of a view controller, which has a single UIWebView. This code compiles fine, but the handleTap method is never called. This seems like it should be simple.

// Configure a ges开发者_JAVA技巧ture recognizer to detect taps in the web view
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap)];
[self.myWebView addGestureRecognizer:singleTap];    
[singleTap release];

[super viewDidLoad];


Set your view controller as the recognizer delegate:

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap)];
recognizer.delegate = self;
[self.myWebView addGestureRecognizer:recognizer];

And enable simultaneous gesture recognition (as the UIWebView probably sets a few recognizers itself, yours are skipped) :

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


Since the UIWebView does already handle a lot of touch events, it might be a bit tricky to add your own gesture recognizer.

I don't know if the user has to interact with your web view but you might try to add a UIView on top of the UIWebView and add the gesture recognizer to this view. It should still propagate unrecognized touch events to the underlying web view, thus leaving its interactivity intact.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜