How do I get the coordinates for finger tapping in UIView?
Asked Answered
A

6

9

How do I get the coordinates for finger tapping in UIView? (I would prefer not to use a big array of buttons)

Thank you

Accommodate answered 24/5, 2011 at 16:43 Comment(0)
J
38

There’re two ways to accomplish this. If you’ve already got a subclass of UIView that you’re using, you can just override the -touchesEnded:withEvent: method on that subclass, like this:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *aTouch = [touches anyObject];
    CGPoint point = [aTouch locationInView:self];
    // point.x and point.y have the coordinates of the touch
}

If you’re not already subclassing UIView, though, and the view is owned by a view controller or whatever, then you can use a UITapGestureRecognizer, like this:

// when the view's initially set up (in viewDidLoad, for example)
UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
[someView addGestureRecognizer:rec];
[rec release];

// elsewhere
- (void)tapRecognized:(UITapGestureRecognizer *)recognizer
{
    if(recognizer.state == UIGestureRecognizerStateRecognized)
    {
        CGPoint point = [recognizer locationInView:recognizer.view];
        // again, point.x and point.y have the coordinates
    }
}
Jugulate answered 24/5, 2011 at 16:51 Comment(1)
touchesEnded:withEvent: can be used from UIViewController too, as that is also derived from UIResponder.Deberadeberry
E
4

Swift 3 answer

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapAction(_:)))
yourView.addGestureRecognizer(tapGesture)


func tapAction(_ sender: UITapGestureRecognizer) {

      let point = sender.location(in: yourView)


}
Eager answered 16/8, 2017 at 13:4 Comment(0)
K
3

I assume you mean recognizing gestures (and touches). The best place to start looking for such a broad question is Apple's sample code Touches. It walks through a good amount of information.

Kingston answered 24/5, 2011 at 16:47 Comment(0)
D
2
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:myView];
    NSLog("%lf %lf", touchPoint.x, touchPoint.y);
}

You need to do something like this. touchesBegan:withEvent: is a method of UIResponder from which UIView and UIViewController both are derived. If you google for this method then you will find several tutorials. MoveMe sample from Apple is a good one.

Deberadeberry answered 24/5, 2011 at 16:51 Comment(0)
S
2
func handleFrontTap(gestureRecognizer: UITapGestureRecognizer) {
    print("tap working")
    if gestureRecognizer.state == UIGestureRecognizerState.Recognized
    { 
      `print(gestureRecognizer.locationInView(gestureRecognizer.view))`
    }
}
Sundial answered 12/4, 2016 at 4:51 Comment(0)
D
0

Swift 5.6:

You can override the following in your UIResponder (which both UIView and UIViewController inherit from):

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let point = touch.location(in: someView)
        print("x = \(point.x), y = \(point.y)")
    }
}

Or in your gesture recognizer handler:

@objc func handleLongPress(gestureRecognizer: UILongPressGestureRecognizer) {
    let point = gestureRecognizer.location(in: someView)
    print("x = \(point.x), y = \(point.y)")

}
Dundalk answered 27/6, 2022 at 16:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.