skytz
skytz

Reputation: 2201

Detect if touch stopped on the screen

The question is simple but extremely complicated: in UIResponder there are 4 methods for handling touches.

- touchesEnded:withEvent:event
– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesCancelled:withEvent:

How do I detect if a touch stopped on the screen?

The problem is that I have to detect if something moved under a stationary touch (not cancelled or ended. It just doesn't move) And because it doesn't move, none of these methods gets called.

My idea was this:

I could add the touches to a NSMutableArray but then I'd have to update it for any touch move (and that's a lot). Also this creates more problems, I need to detect which of the touches stopped and if any ended. And because I get an NSSet from UIResponder, I don't have an organized array so... all kinds of problems.

I'm waiting for ideas.

Upvotes: 1

Views: 1074

Answers (2)

skytz
skytz

Reputation: 2201

i fixed it..and also found out something really cool about UITouch

what i did: in

 – touchesBegan:withEvent:
for(UITouch*touch in touches){
[touchesSet addObject:touch]; } //touchesSet is a set that i store all the touches on the screen

in

- touchesEnded:withEvent:

for(UITouch*touch in touches){
[touchesSet removeObject:touch]; }

in

– touchesCancelled:withEvent:
[touchesSet removeAllObjects];

doing this i have a NSSet of all the touches on the screen at any given time, with position and UITouchPhase

Upvotes: 1

Rob
Rob

Reputation: 438277

The solution depends a little upon what you're trying to do (and you don't really describe what business problem or user experience you're going for). But assuming you're just trying to detect when a continuous gesture paused but hadn't been completed:

You could have touchesMoved keep track of where and when it was last invoked. E.g. if you have a subclassed gesture recognizer, give it a property of CGPoint lastLocation or something like that which you could inquire upon.

You could then setup a NSTimer that would be triggered a certain amount of time later, which would test for your "stopped" condition. E.g. if your NSTimer is called every 0.1 seconds and you're waiting for no change in location for, say 1 second, then that would qualify as a stopped condition.

And if you're looking to see if "something moved under a stationary touch", you could add this to your NSTimer routine.

Upvotes: 0

Related Questions