ios - 获取接触点位置

标签 ios uiwebview touch

我有 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/

相关文章:

ios - 在 iOS App 中,如何同时滚动两个 UIView

ios - 更改 UIWebview 框架会更改 iOS9 中的 ScrollView

ios - webkit 溢出滚动 :touch not working if div is too small

javascript - 阻止 body 滚动,但阻止触摸设备上的任何父 div 滚动

ios - 如何在 UItextfield 中快速添加永久占位符?

ios - 如何从 REST API search.list 结果中过滤掉 YouTube 的内容屏蔽视频

javascript - Parse.com Cloud Code 上的多对多关系

javascript - 我可以让触摸事件通过一个层吗?

javascript - 如何正确地退出一个函数?

Objective-C -- NSObject isEqual, vs. == 比较?