原来我自定义过UITextView,然后将捕获 touchesBegan,touchesMoved和touchesEnded的事件后交给父层去处理,这样就可以UITextView上进行手势翻页 等处理,今天想要故伎重演在UIWebView上,结果失败了。后来用了一种最最简单的办法实现了。
你猜对了,是UITapGestureRecognizer,贴上代码。
在UIViewController中,加入协议UIGestureRecognizerDelegate,然后.m文件里加入以下代码:
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; [self.view addGestureRecognizer:singleTap]; //这个可以加到任何控件上,比如你只想响应WebView,我正好填满整个屏幕singleTap.delegate = self; singleTap.cancelsTouchesInView = NO; [singleTap release];
然后有一个关键的,要实现一个方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; }
最后,响应的方法中,可以获取点击的坐标哦!
-(void)handleSingleTap:(UITapGestureRecognizer *)sender{ CGPoint point = [sender locationInView:self.view]; NSLog(@"handleSingleTap!pointx:%f,y:%f",point.x,point.y); }
好了,得到启发了吧,也能获取到坐标了。不过如果你要直接判断手势方向之类的,比如向左或向右轻扫,可以使用UISwipeGestureRecognizer类,具体可参考这编文章:http://o0o0o0o.iteye.com/blog/875333
时间: 2024-10-11 12:31:05