Sunday, December 6, 2009

A Better Two-Finger Rotate Gesture

Posted by Hidayat Hidayat at 7:06 PM
A while back, I posted a mostly functional, but imperfect sample code for doing a two-finger rotate gesture. I've been meaning to revisit this for some time to get it working correctly.

Tonight, I finally found some time to do so, so I present a better, fully-functional two-finger rotate gesture sample code project. This version allows you to rotate 360° or more without problems. Rather than relying on the order of the touches and the overall rotation angle, I just calculate the angle between the fingers' current location and their previous location.

Screen shot 2009-12-06 at 10.05.04 PM.png
The new version is actually quite a bit simpler than the previous one. Since each instance of UITouch contains both its current location and its previous location, we don't even need to keep track of anything. The old version, in addition to not working correctly past 180° was working much harder than it needed to. The only new thing in this version is that the function that calculates the angle between the two lines looks at the slope of both lines and returns negative or positive values based on which slope is larger.


Post a Comment

Related Post


Copyright © 2011 Next Iphone | Store Mobile Phone Store