我的代码里是这样用的:
else if (recognizer.state == UIGestureRecognizerStateChanged)
{
CGPoint velocity = [recognizer velocityInView:recognizer.view];
SpriteAct act = lastAct;
//NSLog(@"velocity x=%f, y=%f", velocity.x, velocity.y);
if (fabs(velocity.x) > fabs(velocity.y))
{
//left or righ
if(velocity.x > 0)
{
//NSLog(@"gesture went left");
act = ActLeftDirection;
}
else
{
//NSLog(@"gesture went right");
act = ActRightDirection;
}
}
else if( fabs(velocity.x) < fabs(velocity.y))
{
//up or down.
if (velocity.y>0)
{
//NSLog(@"gesture went up");
act = ActUpDirection;
}
else
{
//NSLog(@"gesture went down");
act = ActDownDirection;
}
}
else
{
#ifdef DYNAMIC_CALC_DIRECTION
//last act.
if(velocity.x*lastGestureVelocity.x + velocity.y*lastGestureVelocity.y > 0)
{
//NSLog(@"gesture went in the same direction");
}
else
{
//NSLog(@"gesture went in the opposite direction");
}
#else
act = lastAct;
#endif
}
对于几种UIGestureRecognizer的用法请参考下面的链接:
http://efreedom.com/Question/1-5187502/Can-Capture-Direction-Panned-Using-UIPanGestureRecognizer
http://www.raywenderlich.com/2343/how-to-drag-and-drop-sprites-with-cocos2d
http://ddesktop.googlecode.com/svn-history/r13/trunk/DDesktopClient/Classes/RemoteViewController.m
通过UIPanGestureRecognizer 获得velocity & direction
最新推荐文章于 2021-04-09 11:48:01 发布