iPhone touch screen events
in iPhone SDK there are touchesBegan, touchesEnded and touchesMoved functions that are called when a touch event appears. My problem is that when I put a finger on the screen and I'm not moving it there is no way to know if there is a finger on the screen or not. Is there a way to get current touch screen state?
In any of touchesBegan, touchesEnded and touchesMoved functions I can use fallowing code to know the state for every touch, but outside them I can't:|
NSSet *allTouches = [event allTouches];
for(GLuint index = 0; index < [allTouches count]; ++index)
{
UITouch *touch = [[allTouches allObjects] objectAtIndex:index];
if([touch phase] != UITouchPhaseCancelled)
{
CGPoint touchPoi开发者_StackOverflow中文版nt = [touch locationInView:self];
//do something with touchPoint that has state [touch phase]
}
}
You should keep a list of all points where the touches currently are. Make sure you don't just retain the UIEvent
or UITouch
objects (they're not guaranteed to live long) - instead, create your own data data structure. Odds are all you need is to keep track of the points where touches are currently down.
If you don't want to fix your broken design, why doesn't keeping a running list of active touches solve your problem?
NSMutableSet *touches;
In touchesBegan you add them, in touchesEnded and touchesCancelled you remove them...
You might use a "global" object (i.e. singleton) to track those and synchronize data access. I cannot see how querying this set would be different than asking UIKit directly for this information.
If you need information on touches on standard UIKit objects (i.e. UIButton), you probably need to subclass those - and I am not sure if you can get the desired effect with all classes (as they - or their subviews - could handle touches in any way they want).
精彩评论