iPhone SDK – Get Current Touch Location

16/04/2010

Quick one today, I’m working on something new. It neeeded the last touch location to display some user input. It took me a little while to figure out how to call the location of the currently touched location, but its simple.

Just paste the following method into a viewcontroller:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        // get touch event
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow];
        NSLog(@”Position of touch: %.3f, %.3f”, pos.x, pos.y);
}

thats it! Check the Console/Log to see the output.
Note, using this over a tableviewcontroller is a different story, that is something I am yet to need, or figure out.

Screenshot2010-04-13at21.10.23.HdXB78fAPxSp.jpg

View Comments

  1. 7/07/2010Toni Castillo say:

    Thanks!

Write a comment:

blog comments powered by Disqus