Question

I have a NSObject that moves around the screen using vx (rate at which it moves horizontally) and vy (rate at which it moves vertically). I want to know if I made another of the same NSObject, how I could make it move in a perfect circle, (being able to change the diameter, which direction around the circle and how fast it moves around the circle path) Thanks

Was it helpful?

Solution

 //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"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top