6

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
AwesomeTN
  • 398
  • 2
  • 5
  • 16

3 Answers3

15

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

Sample Project

icodebuster
  • 8,890
  • 7
  • 62
  • 65
2

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

Diego A. Rincon
  • 747
  • 1
  • 8
  • 25
0

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.

Ander
  • 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