Search code examples
iosobjective-cuitouch

Confused about ios touches code


-(void)ccTouchesBegan...

UITouch* touch = [touches anyObject];

CGPoint location = [touch locationInView:[touch view]];

Can someone please explain in detail what exactly is going on in these two lines of code. Thanks


Solution

  • UITouch *touch = [touches anyObject];
    

    touches is a NSSet of UITouch. The code simply gets one object from touches and assigns it to a variable named touch. This is implicitly assuming that the NSSet hold only one element.

    CGPoint location = [touch locationInView:[touch view]];
    

    the above line gets the (x,y) coordinates of the touch in the coordinate system of the view that intercepted the touch. CGPoint is nothing more than a C struct with two float values, x and y.

    So bottom line you will obtain the coordinates of the touch in the view.