关于UIWebView不能响应touchesBegan等四个方法的解决案例【可以检测 单击双击】
关于UIWebView不能响应touchesBegan等四个方法的解决案例
做的APP需要在一个UIWebView中随点击位置动态出现button,用touchesBegan等怎么调试都不行,普通的UIView就可以,找了半天终于知道UIWebView不能这样用,那么怎么做呢,只有用UITapGestureRecognizer替代了,其他的手势还可以用UIPinchGestureRecognizer(放大缩小)和UIPanGestureRecognizer(拖动),SO,我们现在以UITagGestureRecongnizer来做,获取webView中的位置。
UITapGestureRecognizer* myTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleTap]; //这个可以加到任何控件上,比如你只想响应WebView,我正好填满整个屏幕
myTap.delegate = self;
myTap.cancelsTouchesInView = NO;
[myTap release];
如果是双击可以这样:
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
doubleTap.numberOfTouchesRequired = 2;
[self.theWebView addGestureRecognizer:doubleTap];
然后有一个关键的,要实现一个方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
最后,响应的方法中,可以获取点击的坐标哦!
-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
CGPoint gesturePoint = [sender locationInView:self.view];
NSLog(@"handleSingleTap!gesturePoint:%f,y:%f",gesturePoint.x,gesturePoint.y);
}