使用背景:在UIWebView 中获取点击事件
引入头文件和delegate就不废话了,自己查去
直接贴代码:
在我的webViewController的viewdidload()里面:
UITapGestureRecognizer *singleRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTapEvent:)];
singleRecognizer.delegate = self;
singleRecognizer.numberOfTapsRequired = 1; // 单击
singleRecognizer.numberOfTouchesRequired = 1;//单指
[wv addGestureRecognizer:singleRecognizer];//wv为我创建的一个UIWebView的变量
[singleRecognizer release];
第一句:创建了个UITapGestureRecognizer类型的手势
第二句:传delegate,为了handleSingleTapEvent回调
第三第四句:设置属性,看一下文档就可以了
第五句:将手势加入到需要的view
- (void) handleSingleTapEvent:(UITapGestureRecognizer *)recognizer{
CGPoint pt = [recognizer locationInView:self.view];
self.singleTapPoint = pt;
MXLog(@"singleTapPoint:(%d,%d)",(NSInteger)pt.x,(NSInteger)pt.y);
// do what you want to do ,you can see ,i love pt
}
warning:
如果这时候你页面上有个button,你点击button的时候发现button不响应UIControlEventTouchUpInside事件了,具体表现就是:你点击button,button陷下去之后就再也没起来,这时候需要看看手势的delegate了,那里有解决方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[UIControl class]]) {
// we touched a button, slider, or other UIControl
return NO; // ignore the touch
}
return YES; // handle the touch
}
原因:响应被手势截获,这时候,判断一下,把响应还给button等玩意
另外:
要实现多指多击等手势操作,需要参考这位仁兄的博客:
http://blog.csdn.net/jasonyuan1986/article/details/6821134
要解决手势共存问题,要参考这位仁兄博客(松花江以南):
http://www.cnblogs.com/iphone520/archive/2011/10/27/2226548.html
为方便查找,将手势类别写在下面:
UIGestureRecognizer的衍生类別有以下几种:
- UITapGestureRecognizer
- UIPinchGestureRecognizer
- UIRotationGestureRecognizer
- UISwipeGestureRecognizer
- UIPanGestureRecognizer
- UILongPressGestureRecognizer