开发者

How to track the touch vector?

开发者 https://www.devze.com 2022-12-29 12:43 出处:网络
I need to calculate the direction of dragging a touch, to determine if the user is dragging up the screen, or down the screen.

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 wh开发者_JAVA百科at 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?


If you're just talking about detecting swipes, then in OS 3.2, Apple introduced gesture recognizers to make this sort of thing easier in iPad applications. You can also write your own recognizers for other kinds of gestures (like long drags), with the OS doing most of the heavy lifting, and notifying you about the "interesting moments".

According to some reports, it looks like they'll be in 4.0 and available to iPhone, as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消