我有 UIWebView 用于显示文章。我有用于显示 HTML 文章的 UIWebView。当用户触摸 UIWebView 中的某个区域时,UIMenuController 将显示。然后用户选择它显示 UITextView 的注释按钮。如何获取触摸位置?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
// Get the specific point that was touched
CGPoint point = [touch locationInView:wbCont];
NSLog(@"X location: %f", point.x);
NSLog(@"Y Location: %f",point.y);
}
- (void)note:(id)sender {
}
最佳答案
UIWebview 包裹在 UIScrollView 中。因此,触摸事件不会传递到您的方法接收触摸事件的 UIView。
对于你的 UIViewController,它应该实现 UIGestureRecognizerDelegate
然后向您的 webview 添加一个手势:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTest:)];
[tap setDelegate:self];
[self.yourwebview.scrollView addGestureRecognizer:tap];
下一步:
- (void)tapTest:(UITapGestureRecognizer *)sender {
NSLog(@"%f %f", [sender locationInView:self.yourwebview].x, [sender locationInView:self.yourwebview].y);
}
编辑:
shouldRecognizeSimultaneouslyWithGestureRecognizer 也应该返回 YES。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
关于ios - 获取接触点位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19564723/