is it possible to get the x and y coordinates of a touch? If so could someone please provide a very simple example where the coordinates are just logged to the console.
-
3Have you read the UITouch Class reference? – rdelmar Jul 18 '13 at 04:11
3 Answers
Using touchesBegan Event
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
NSLog(@"Touch x : %f y : %f", touchPoint.x, touchPoint.y);
}
This event is triggered when touch starts.
Using Gesture
Register your UITapGestureRecognizer in viewDidLoad:
Method
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizer:)];
[self.view setUserInteractionEnabled:YES];
[self.view addGestureRecognizer:tapGesture];
}
Setting up the tapGestureRecognizer function
// Tap GestureRecognizer function
- (void)tapGestureRecognizer:(UIGestureRecognizer *)recognizer {
CGPoint tappedPoint = [recognizer locationInView:self.view];
CGFloat xCoordinate = tappedPoint.x;
CGFloat yCoordinate = tappedPoint.y;
NSLog(@"Touch Using UITapGestureRecognizer x : %f y : %f", xCoordinate, yCoordinate);
}

- 8,890
- 7
- 62
- 65
-
Going off of the first method you gave, using the touches began, how can I make the x and y positions global variables? – AwesomeTN Jul 18 '13 at 06:15
-
Create a CGPoint variable in your .h file and assign it in the above methoh – icodebuster Jul 18 '13 at 06:50
-
The touches began work great, but I am unable to get touchesEnded to work, isn't almost the exact same thing? – AwesomeTN Jul 18 '13 at 16:55
First you need to add a gesture recognizer to the view you want.
UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myTapRecognizer:)];
[self.myView setUserInteractionEnabled:YES];
[self.myView addGestureRecognizer:myTap];
Then in the gesture recognizer method you make a call to locationInView:
- (void)myTapRecognizer:(UIGestureRecognizer *)recognizer
{
CGPoint tappedPoint = [recognizer locationInView:self.myView];
CGFloat xCoordinate = tappedPoint.x;
CGFloat yCoordinate = tappedPoint.y;
}
You may want to take a look at apple's UIGestureRecognizer Class Reference

- 747
- 1
- 8
- 25
Here's a very basic example (place it inside your view controller):
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
NSLog(@"%@", NSStringFromCGPoint(currentPoint));
}
This triggers every time the touch moves. You can also use touchesBegan:withEvent:
which triggers when a touch starts, and touchesEnded:withEvent:
which triggers when a touch ends (i.e. a finger is lifted).
You can also do this using a UIGestureRecognizer
, which in many cases is more practical.

- 3,688
- 1
- 22
- 24
-
I c/p this into my view controller and I got an error with the NSLog, but it was fixed by changing the string to NSSringFromCGPoint. But I still did not recieve anything in the console, am I missing something? Thanks for your help – AwesomeTN Jul 18 '13 at 05:37
-
You shouldn't have to add any extra code to make this work. Could something else be capturing the touch event (e.g. is there a subview like a button that could be stopping the touch from getting through to the viewController's view)? – Ander Jul 18 '13 at 05:48
-
Essentially, any view on added to the viewController's `self.view` that doesn't have `.userInteractionEnabled = NO` would capture the touch and stop it from going to the methods given in the answer above. – Ander Jul 18 '13 at 05:50