How to track the touch vector?

Posted by mystify on Stack Overflow See other posts from Stack Overflow or by mystify
Published on 2010-05-09T15:47:10Z Indexed on 2010/05/09 15:48 UTC
Read the original article Hit count: 228

Filed under:

I need to calculate the direction of dragging a touch, to determine if the user is dragging up the screen, or down the screen.

Actually pretty simple, right? But:

1) Finger goes down, you get -touchesBegan:withEvent: called

2) Must wait until finger moves, and -touchesMoved:withEvent: gets called

3) Problem: At this point it's dangerous to tell if the user did drag up or down.

My thoughts: Check the time and accumulate calculates vectors until it's secure to tell the direction of touch.

Easy? No. Think about it: What if the user holds the finger down for 5 minutes on the same spot, but THEN decides to move up or down? BANG! Your code would fail, because it tried to determine the direction of touch when the finger didn't move really.

Problem 2: When the finger goes down and stays at the same spot for a few seconds because the user is a bit in the wind and thinks about what to do now, you'll get a lot of -touchesMoved:withEvent: calls very likely, but with very minor changes in touch location.

So my next thought: Do the accumulation in -touchesMoved:withEvent:, but only if a certain threshold of movement has been exceeded.

I bet you have some better concepts in place?

© Stack Overflow or respective owner

Related posts about iphone