这里我将展示如何在UIView上让对象沿着路径动画,我将创建路径并画到UIView上让你能都看见,并沿相同的路径来做动画。
我在添加到屏幕的UIView完成所有的这些…
首先,我们在屏幕上画一条曲线
-
- - ( void ) drawACurvedLine {
-
- UIGraphicsBeginImageContext(CGSizeMake(320,460));
- CGContextRef ctx = UIGraphicsGetCurrentContext();
-
-
- CGContextSetLineWidth(ctx, 1.5);
- CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
-
-
- CGContextMoveToPoint(ctx, 10, 10);
-
-
- CGContextAddQuadCurveToPoint(ctx, 10, 450, 310, 450);
-
- CGContextAddQuadCurveToPoint(ctx, 310, 10, 10, 10);
-
-
- CGContextDrawPath(ctx, kCGPathStroke);
-
-
- UIImage *curve = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
-
- UIImageView *curveView = [[UIImageView alloc] initWithImage:curve];
-
- curveView.frame = CGRectMake(1, 1, 320, 460);
- curveView.backgroundColor = [UIColor clearColor];
- [self addSubview:curveView];
- }
现在我我创建了一个关键帧动画,并添加一个和我们话得线一样的路径。我们还将画一个圈,并沿着路径做动画:
- - (void) animateCicleAlongPath {
-
- CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
-
- pathAnimation.calculationMode = kCAAnimationPaced;
-
-
-
- pathAnimation.fillMode = kCAFillModeForwards;
- pathAnimation.removedOnCompletion = NO;
- pathAnimation.duration = 5.0;
-
- pathAnimation.repeatCount = 1000;
-
-
-
- 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);
-
-
- pathAnimation.path = curvedPath;
- CGPathRelease(curvedPath);
-
-
-
-
- UIGraphicsBeginImageContext(CGSizeMake(20,20));
- CGContextRef ctx = UIGraphicsGetCurrentContext();
-
- CGContextSetLineWidth(ctx, 1.5);
- CGContextSetFillColorWithColor(ctx, [UIColor greenColor].CGColor);
- CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
-
- 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];
-
-
- [circleView.layer addAnimation:pathAnimation forKey:@"moveTheSquare"];
- }
要让所有的都跑起来,你可以使用init函数:
- - (id)initWithFrame:(CGRect)frame {
- if (self = [super initWithFrame:frame]) {
- [self drawACurvedLine];
- [self animateCicleAlongPath];
- }
- return self;
- }
在你的ViewController中像这样写
- - (void)viewDidLoad {
- UIView *customView = [[Canvas alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
- customView.backgroundColor = [UIColor blackColor];
- [self.view addSubview:customView];
- [customView release];
- [super viewDidLoad];
- }
还有,不要忘记添加 Quartz 引用:
- #import <QuartzCore/QuartzCore.h>
我确定有很多更好的方式来做这个事,例如使用CALayers 和添加CGImage 到layers。但是那是一些我没有尝试的东西。上面的例子应该足够让你开始沿着路径做动画。
![Animate along a path Animate along a path](http://blog.devedup.com/wp-content/uploads/2010/03/Screen-shot-2010-03-03-at-21.40.45-161x300.png)
这里是工程拷贝
(译者水平有限,欢迎指正。)
本文转自 arthurchen 51CTO博客,原文链接:http://blog.51cto.com/arthurchen/641572,如需转载请自行联系原作者