3

In UIResponder

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

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

nevan king
  • 112,709
  • 45
  • 203
  • 241
ben
  • 1,020
  • 1
  • 15
  • 27
  • Were you not able to find this by reading the Apple iOS reference library docs? – mk12 Jul 22 '10 at 03:26
  • 3
    @Mk12 you could say the same thing for your 89 questions, so that's not a very fair question to ask. It's okay to ask for help. – iwasrobbed Jul 22 '10 at 05:04
  • I'm just saying he should check there first. If he did and his answer is no, then that's fine. – mk12 Jul 22 '10 at 20:35
  • 1
    It's also not fair to dismiss all of my questions as trivial details found in the Apple docs. I have had people point me to the documentation a couple times, but I doubt you've read them all. – mk12 Jul 30 '10 at 17:57

2 Answers2

6

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.

Thomas Anagrius
  • 926
  • 5
  • 15
  • i still have a question,when i touched at a UISlider in a uivew,and moved the indicator of the UISlider,[touches allObject] and [event allTouches] both couldn't send a responder.why?thank you. – ben Jul 23 '10 at 07:03
3

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.
Coyote
  • 2,454
  • 26
  • 47