Mircea Ispas
Mircea Ispas

Reputation: 20780

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 touchPoint = [touch locationInView:self];
        //do something with touchPoint that has state [touch phase]
    }
}

Upvotes: 0

Views: 1237

Answers (2)

Eiko
Eiko

Reputation: 25632

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).

Upvotes: 0

Jesse Beder
Jesse Beder

Reputation: 34034

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.

Upvotes: 1

Related Questions