Question

In UIResponder

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

What's the difference between [event allTouches] and [touches allObjects]?

Was it helpful?

Solution

To my understanding it is as follows:

[event allTouches] returns all touches that are part of the event. Some of those touches might be meant for another UIResponder.

For instance you might click in two view at the same time and the responder associated with each view will get called with all the touches of the event.

[touches allObject] only contains touches ment for this responder. And is thus in most cases what you are after.

OTHER TIPS

The event gives access to all the touches through allTouches. Even the touches currently not active (not moving not starting not ending and not being canceled).

for (UITouch* touch in [[event allTouches]allObjects]) //loops through the list of all the current touches

touches is the list of all the touches that have changed and are eligible for the current event.

for (UITouch* touch in [touches allObjects]) //loops through the list of all changed touches for this event.

So for touchesBegan:withEvent:

  • If one finger touches the screen touches and [event allTouches] will have the same content.
  • If a second finger touches the screen touches will provide the UITouch associated with this finger and [event allTouches] will provide the UITouch for this finger and the one already touching the screen.
  • If 2 additional fingers touch the screen at the "same" time touches will provide the UITouch for the 2 additional fingers and [event allTouches] will provide the 4 UITouch instances.

Now in the case of touchesEnded:withEvent:

  • If one finger is lifted, touches will give access to the UITouch instance associated with that finger while [event allTouches] will provide the 4 UITouch instances, even the one of the ending touch.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top