Sunday, November 1, 2009

Touch Events and the iPhone SDK

There are four events for touches in the iPhone.

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

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

(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event


(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

In the excellent Stanford ITunes U course CS193P they mention that two important things about touchesCancelled cause a number of calls to Apple support: “Cancelled” has two letter L’s in it and touchesCancelled gets called at times when a developer may think that touchesEnded might be called. So, as a safeguard, I now just call touchesEnded from touchesCancelled as a default using this code:

[self touchesEnded:touches withEvent:event];

In general, touchesCancelled will be called when a phone call arrives or when the user presses the home button on the phone to quit the app. If your application needs to do something special there, then be sure to put some code in touchesCancelled. Examples of things you might want to put over there include returning your application to its default state.
Right now, Isabel and I are working on a game that has an NSTimer and also responds to UITouch events. I’m experimenting to see if the timer frequency and the event loop frequency can be set to values that conflict. I’m guessing that we probably can, but I’m also guessing that normal people cannot move their fingers fast enough to make a difference. However, people who drink a lot of Sun Drop....well, that’s another story.

No comments:

Post a Comment