How to get coordinates for finger listening in UIView?

How to get coordinates for finger listening in UIView? (I would prefer not to use a large set of buttons)

thanks

+7
source share
5 answers

Here are two ways to achieve this. If you already have the UIView subclass that you are using, you can simply override the -touchesEnded:withEvent: method in this subclass, for example:

 - (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 have not yet subclassed the UIView, and the view belongs to a view controller or the like, then you can use UITapGestureRecognizer, for example:

 // when the view 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 } } 
+30
source

I suppose you mean gesture recognition (and touch). The best place to look for such a broad question is the Apple Touches sample code. He looks through a lot of information.

+2
source
 - (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: - the UIResponder method from which the UIView and UIViewController . If you are Google for this method, you will find several tutorials. Apple's MoveMe pattern is good.

+2
source
 func handleFrontTap(gestureRecognizer: UITapGestureRecognizer) { print("tap working") if gestureRecognizer.state == UIGestureRecognizerState.Recognized { `print(gestureRecognizer.locationInView(gestureRecognizer.view))` } } 
+2
source

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) } 
0
source

All Articles