Search code examples
iosuitouch

How to enumerate 2 touch points objective C


To find co ordinates of 2 touch points, then find distance between 2 points and hence to generate center to rotate around, I'm using following piece of code. I'm not sure if this is best way to do it.

- (void) touchesMoved:(NSSet *)touches withEvent: (UIEvent *)event

<SOME CODE>


if([touches count] == 2){
    for(touch in touches){
        if(self.pt1Flag){
            self.pt2=[touch locationInView:self.superview];
            self.pt2Flag = YES;
            self.pt1Flag = NO;
        }
        else{
            self.pt1 = [touch locationInView:self.superview];
            self.pt1Flag = YES;
            self.pt2Flag = NO;
        }

    }
}

self.delta = [self lengthDifferencePoint:self.pt1 andPoint:self.pt2 equiDistantFromPoint:self.center];

<SOME CODE>

}

There is mostly for single finger touch. Is there a proper way to do it? Also I understand there is

 [touches enumerateObjectsUsingBlock:^(id obj, BOOL *stop){
}];

can somebody please explain usage of this block to find coordinates of touch point or any better/cleaner solution than mine.


Solution

  • enumerateObjectsUsingBlock will not help you in this case, you are not filtering or doing any sort of process in the objects. In fact the for-in loop itself also enumerates the objects.

    But if you know there are only 2 objects you don't need to traverse them, you can simply:

        NSArray *touchesArray = [touches allObjects];
        self.pt1 = [touchesArray[0] locationInView:self.superview];
        self.pt2 = [touchesArray[1] locationInView:self.superview];