0

我有 UIWebView 来显示文章。我有 UIWebView 用于显示 HTML 文章。当用户触摸 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  {


}
4

5 回答 5

4

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;
}
于 2013-10-24T12:42:54.310 回答
1

尝试这个 :-

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    firstTouch = [touch locationInView:self.view];

    NSLog(@" CHECKING CGPOINT %@", NSStringFromCGPoint(firstTouch));


}

对我来说很好;-)

于 2013-10-24T11:42:32.670 回答
0
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

 UITouch *touch = [touches anyObject];   
    NSLog(@"X location: %f",touch.view.position.x);
    NSLog(@"Y Location: %f",touch.position.y);
}
于 2013-10-24T12:29:49.263 回答
0

只有一种真正的故障安全方法可以做到这一点(据我所知):

@interface MyWebView : UIWebView
@end

@implementation MyWebView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if ([self pointInside:point withEvent:event])
    {
        NSLog(@"Touched inside!");
    }

    return [super hitTest:point withEvent:event];
}

@end

Web 视图非常复杂,因此覆盖这些UIResponder方法或添加手势识别器将不起作用。不幸的是,hitTest:withEvent:每次触摸都会被调用 3 次,所以你必须处理它。

于 2013-10-24T12:02:25.557 回答
0
-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event

{

     NSSet *touches = [event allTouches];

    UITouch *touch = [touches anyObject];

    //UITouch *touch = [[event touchesForView:textview] anyObject];
     CGPoint location = [touch locationInView: wbCont];

     CGPoint previousLocation = [touch previousLocationInView:textview];

}
于 2013-10-24T11:43:47.780 回答