Search code examples
iosobjective-ccoordinatestouchesbegan

Coordinates of screen touch aren't working


So I'm trying to get the coordinates of a screen touch. Here is the code I'm using:

UITouch *touched = [[event allTouches] anyObject];
location = [Touch locationInView:touched.view];
NSLog(@"x=%.2f y=%.2f", location.x, location.y);

All I need to test for is the Y coordinate, so I'm printing out the Y coordinate on the screen to see what it's saving as the location.y and right now it's saving it as 0.000000

How do I get this to work properly so it's not saving 0 as the location.y


Solution

  • It depend of your views hierarchy.

    I will assume you meant

    location = [touched locationInView:touched.view];
    

    and not

    location = [Touch locationInView:touched.view];
    

    If you want the location in your controller view, you should use

    [touched locationInView:self.view];
    

    Your touch.view can refer to a subview of your view hierarchy in which the coordinate could be 0, 0.

    EDIT: It seem issue was just related to Touch instead of touched