user2807197
user2807197

Reputation: 207

Get touch point location

I have UIWebView for display the articles. I have UIWebView for displays HTML articles. When user touch some area in UIWebView, UIMenuController will display. Then user select note button it displays UITextView. How to get touch location?

- (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  {


}

Upvotes: 0

Views: 10786

Answers (5)

TreeTree
TreeTree

Reputation: 557

UIWebview is wrapped inside a UIScrollView. Therefore the touch events do not go through to the UIView where your method receives the touch events.

For your UIViewController it should implement UIGestureRecognizerDelegate

Then add a gesture to your webview:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTest:)];
[tap setDelegate:self];
[self.yourwebview.scrollView addGestureRecognizer:tap]; 

Next:

- (void)tapTest:(UITapGestureRecognizer *)sender {
    NSLog(@"%f %f", [sender locationInView:self.yourwebview].x,  [sender locationInView:self.yourwebview].y);
}

Edit:

shouldRecognizeSimultaneouslyWithGestureRecognizer should return YES as well.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

Upvotes: 4

Pravin
Pravin

Reputation: 44

- (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);
}

Upvotes: 0

Guy Kogus
Guy Kogus

Reputation: 7341

There's only one truly fail-safe way to do this (that I know of):

@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 views are extremely complex, so overriding those UIResponder methods or adding gesture recognisers won't work. Unfortunately, hitTest:withEvent: gets called 3 times on each touch, so you'll have to deal with that.

Upvotes: 0

user2645897
user2645897

Reputation:

-(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];

}

Upvotes: 0

Rana
Rana

Reputation: 451

Try this :-

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

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


}

works fine for me ;-)

Upvotes: 1

Related Questions