如何让NSObject有一个圆路径

时间:2013-10-02 16:40:04

标签: xcode path geometry nsobject motion

我有一个NSObject,它使用vx(水平移动的速率)和vy(垂直移动的速率)在屏幕上移动。我想知道我是否制作了另一个相同的NSObject,我怎么能让它在一个完美的圆圈中移动,(能够改变直径,围绕圆圈的方向以及它绕圆圈路径的移动速度) 感谢

1 个答案:

答案 0 :(得分:0)

 //Prepare the animation - we use keyframe animation for animations of this complexity
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation        animationWithKeyPath:@"position"];
//Set some variables on the animation
pathAnimation.calculationMode = kCAAnimationPaced;
//We want the animation to persist - not so important in this case - but kept for     clarity
//If we animated something from left to right - and we wanted it to stay in the new position,
//then we would need these parameters
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = 5.0;

//Lets loop continuously for the demonstration
pathAnimation.repeatCount = 1;

//Setup the path for the animation - this is very similar as the code the draw the line
//instead of drawing to the graphics context, instead we draw lines on a CGPathRef
//CGPoint endPoint = CGPointMake(310, 450);
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, 10, 10);

 //change the rectangle to adjust diameter and position
     CGRect rectangle = CGRectMake(50,250,220,150);

CGPathAddEllipseInRect(curvedPath, NULL, rectangle);



//Now we have the path, we tell the animation we want to use this path - then we release the path
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);



[myObject.layer addAnimation:pathAnimation forKey:@"moveTheObject"];