<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>devedup.com &#187; CAKeyframeAnimation</title>
	<atom:link href="http://blog.devedup.com/index.php/tag/cakeyframeanimation/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.devedup.com</link>
	<description></description>
	<lastBuildDate>Sun, 09 Oct 2011 16:00:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>iPhone: Animate an object along a path</title>
		<link>http://blog.devedup.com/index.php/2010/03/03/iphone-animate-an-object-along-a-path/</link>
		<comments>http://blog.devedup.com/index.php/2010/03/03/iphone-animate-an-object-along-a-path/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 21:39:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA["animate along a path"]]></category>
		<category><![CDATA["animation path"]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[CAKeyframeAnimation]]></category>
		<category><![CDATA[UIGraphicsBeginImageContext]]></category>

		<guid isPermaLink="false">http://blog.devedup.com/?p=166</guid>
		<description><![CDATA[Here i will show you how to animate an object along a path on a UIView. I will create the path and draw it onto the UIView so that you can see it, and then use the same path for the animation. I&#8217;m doing all of this within a UIView that i have added to [...]]]></description>
			<content:encoded><![CDATA[<p>Here i will show you how to animate an object along a path on a UIView. I will create the path and draw it onto the UIView so that you can see it, and then use the same path for the animation.</p>
<p>I&#8217;m doing all of this within a UIView that i have added to my screen&#8230; </p>
<p><img src="http://blog.devedup.com/wp-content/uploads/2010/03/Screen-shot-2010-03-03-at-21.40.45-161x300.png" alt="Animate along a path" title="Animate along a path" width="161" height="300" class="aligncenter size-medium wp-image-171" /></p>
<p>Firstly, we will draw a curved line on the screen&#8230;.</p>
<pre class="brush: objc;">
//This draws a quadratic bezier curved line right across the screen
- ( void ) drawACurvedLine {
	//Create a bitmap graphics context, you will later get a UIImage from this
	UIGraphicsBeginImageContext(CGSizeMake(320,460));
	CGContextRef ctx = UIGraphicsGetCurrentContext();

	//Set variables in the context for drawing
	CGContextSetLineWidth(ctx, 1.5);
	CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);

	//Set the start point of your drawing
	CGContextMoveToPoint(ctx, 10, 10);
	//The end point of the line is 310,450 .... i'm also setting a reference point of 10,450
	//A quadratic bezier curve is drawn using these coordinates - experiment and see the results.
	CGContextAddQuadCurveToPoint(ctx, 10, 450, 310, 450);
	//Add another curve, the opposite of the above - finishing back where we started
	CGContextAddQuadCurveToPoint(ctx, 310, 10, 10, 10);

	//Draw the line
	CGContextDrawPath(ctx, kCGPathStroke);

	//Get a UIImage from the current bitmap context we created at the start and then end the image context
	UIImage *curve = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();

	//With the image, we need a UIImageView
	UIImageView *curveView = [[UIImageView alloc] initWithImage:curve];
	//Set the frame of the view - which is used to position it when we add it to our current UIView
	curveView.frame = CGRectMake(1, 1, 320, 460);
	curveView.backgroundColor = [UIColor clearColor];
	[self addSubview:curveView];
}
</pre>
<p>Now we will create a keyframe animation, and a path that is the same as the line we just drew. We will also draw a circle, and animate it along that path:</p>
<pre class="brush: objc;">
- (void) animateCicleAlongPath {
	//Prepare the animation - we use keyframe animation for animations of this complexity
	CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@&quot;position&quot;];
	//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 = 1000;

	//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);
	CGPathAddQuadCurveToPoint(curvedPath, NULL, 10, 450, 310, 450);
	CGPathAddQuadCurveToPoint(curvedPath, NULL, 310, 10, 10, 10);

	//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);

	//We will now draw a circle at the start of the path which we will animate to follow the path
	//We use the same technique as before to draw to a bitmap context and then eventually create
	//a UIImageView which we add to our view
	UIGraphicsBeginImageContext(CGSizeMake(20,20));
	CGContextRef ctx = UIGraphicsGetCurrentContext();
	//Set context variables
	CGContextSetLineWidth(ctx, 1.5);
	CGContextSetFillColorWithColor(ctx, [UIColor greenColor].CGColor);
	CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
	//Draw a circle - and paint it with a different outline (white) and fill color (green)
	CGContextAddEllipseInRect(ctx, CGRectMake(1, 1, 18, 18));
	CGContextDrawPath(ctx, kCGPathFillStroke);
	UIImage *circle = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();

	UIImageView *circleView = [[UIImageView alloc] initWithImage:circle];
	circleView.frame = CGRectMake(1, 1, 20, 20);
	[self addSubview:circleView];

	//Add the animation to the circleView - once you add the animation to the layer, the animation starts
	[circleView.layer addAnimation:pathAnimation forKey:@&quot;moveTheSquare&quot;];
}
</pre>
<p>To get all this running, you can use this init method:</p>
<pre class="brush: objc;">
- (id)initWithFrame:(CGRect)frame {
	if (self = [super initWithFrame:frame]) {
		[self drawACurvedLine];
		[self animateCicleAlongPath];
    }
    return self;
}
</pre>
<p>and use something like this in your ViewController&#8230;.</p>
<pre class="brush: objc;">
- (void)viewDidLoad {
	UIView *customView = [[Canvas alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
	customView.backgroundColor = [UIColor blackColor];
	[self.view addSubview:customView];
	[customView release];
    [super viewDidLoad];
}
</pre>
<p>Also&#8230;don&#8217;t forget to add your Quartz import:</p>
<pre class="brush: objc;">
#import &lt;QuartzCore/QuartzCore.h&gt;
</pre>
<p>I&#8217;m sure there are lots of better ways of doing this, such as using CALayers and adding CGImage to the layers. But that&#8217;s something I haven&#8217;t tried yet. The example above should be enough to get you started with animation along a path.</p>
<p>Here is a copy of the XCode project:<br />
<a href="http://blog.devedup.com/wp-content/uploads/2010/06/AnimateAlongAPath.zip"><img src="http://blog.devedup.com/wp-content/uploads/2010/03/xcodeProject.png" alt="xcodeProject" title="xcodeProject" width="70" height="59" class="aligncenter size-full wp-image-218" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.devedup.com/index.php/2010/03/03/iphone-animate-an-object-along-a-path/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

