iPhone: 对象沿路径动画

这里我将展示如何在UIView上让对象沿着路径动画,我将创建路径并画到UIView上让你能都看见,并沿相同的路径来做动画。

我在添加到屏幕的UIView完成所有的这些… 

首先,我们在屏幕上画一条曲线

 

 
 
  1. //This draws a quadratic bezier curved line right across the screen  
  2. - ( void ) drawACurvedLine {  
  3.     //Create a bitmap graphics context, you will later get a UIImage from this  
  4.     UIGraphicsBeginImageContext(CGSizeMake(320,460));  
  5.     CGContextRef ctx = UIGraphicsGetCurrentContext();  
  6.  
  7.     //Set variables in the context for drawing  
  8.     CGContextSetLineWidth(ctx, 1.5);  
  9.     CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);  
  10.  
  11.     //Set the start point of your drawing  
  12.     CGContextMoveToPoint(ctx, 10, 10);  
  13.     //The end point of the line is 310,450 .... i'm also setting a reference point of 10,450  
  14.     //A quadratic bezier curve is drawn using these coordinates - experiment and see the results.  
  15.     CGContextAddQuadCurveToPoint(ctx, 10, 450, 310, 450);  
  16.     //Add another curve, the opposite of the above - finishing back where we started  
  17.     CGContextAddQuadCurveToPoint(ctx, 310, 10, 10, 10);  
  18.  
  19.     //Draw the line  
  20.     CGContextDrawPath(ctx, kCGPathStroke);  
  21.  
  22.     //Get a UIImage from the current bitmap context we created at the start and then end the image context  
  23.     UIImage *curve = UIGraphicsGetImageFromCurrentImageContext();  
  24.     UIGraphicsEndImageContext();  
  25.  
  26.     //With the image, we need a UIImageView  
  27.     UIImageView *curveView = [[UIImageView alloc] initWithImage:curve];  
  28.     //Set the frame of the view - which is used to position it when we add it to our current UIView  
  29.     curveView.frame = CGRectMake(1, 1, 320, 460);  
  30.     curveView.backgroundColor = [UIColor clearColor];  
  31.     [self addSubview:curveView];  

 现在我我创建了一个关键帧动画,并添加一个和我们话得线一样的路径。我们还将画一个圈,并沿着路径做动画:

 

 
 
  1. - (void) animateCicleAlongPath {  
  2.     //Prepare the animation - we use keyframe animation for animations of this complexity  
  3.     CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];  
  4.     //Set some variables on the animation  
  5.     pathAnimation.calculationMode = kCAAnimationPaced;  
  6.     //We want the animation to persist - not so important in this case - but kept for clarity  
  7.     //If we animated something from left to right - and we wanted it to stay in the new position,  
  8.     //then we would need these parameters  
  9.     pathAnimation.fillMode = kCAFillModeForwards;  
  10.     pathAnimation.removedOnCompletion = NO;  
  11.     pathAnimation.duration = 5.0;  
  12.     //Lets loop continuously for the demonstration  
  13.     pathAnimation.repeatCount = 1000;  
  14.  
  15.     //Setup the path for the animation - this is very similar as the code the draw the line  
  16.     //instead of drawing to the graphics context, instead we draw lines on a CGPathRef  
  17.     CGPoint endPoint = CGPointMake(310, 450);  
  18.     CGMutablePathRef curvedPath = CGPathCreateMutable();  
  19.     CGPathMoveToPoint(curvedPath, NULL, 10, 10);  
  20.     CGPathAddQuadCurveToPoint(curvedPath, NULL, 10, 450, 310, 450);  
  21.     CGPathAddQuadCurveToPoint(curvedPath, NULL, 310, 10, 10, 10);  
  22.  
  23.     //Now we have the path, we tell the animation we want to use this path - then we release the path  
  24.     pathAnimation.path = curvedPath;  
  25.     CGPathRelease(curvedPath);  
  26.  
  27.     //We will now draw a circle at the start of the path which we will animate to follow the path  
  28.     //We use the same technique as before to draw to a bitmap context and then eventually create  
  29.     //a UIImageView which we add to our view  
  30.     UIGraphicsBeginImageContext(CGSizeMake(20,20));  
  31.     CGContextRef ctx = UIGraphicsGetCurrentContext();  
  32.     //Set context variables  
  33.     CGContextSetLineWidth(ctx, 1.5);  
  34.     CGContextSetFillColorWithColor(ctx, [UIColor greenColor].CGColor);  
  35.     CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);  
  36.     //Draw a circle - and paint it with a different outline (white) and fill color (green)  
  37.     CGContextAddEllipseInRect(ctx, CGRectMake(1, 1, 18, 18));  
  38.     CGContextDrawPath(ctx, kCGPathFillStroke);  
  39.     UIImage *circle = UIGraphicsGetImageFromCurrentImageContext();  
  40.     UIGraphicsEndImageContext();  
  41.  
  42.     UIImageView *circleView = [[UIImageView alloc] initWithImage:circle];  
  43.     circleView.frame = CGRectMake(1, 1, 20, 20);  
  44.     [self addSubview:circleView];  
  45.  
  46.     //Add the animation to the circleView - once you add the animation to the layer, the animation starts  
  47.     [circleView.layer addAnimation:pathAnimation forKey:@"moveTheSquare"];  

要让所有的都跑起来,你可以使用init函数:

 

 
 
  1. - (id)initWithFrame:(CGRect)frame {  
  2.     if (self = [super initWithFrame:frame]) {  
  3.         [self drawACurvedLine];  
  4.         [self animateCicleAlongPath];  
  5.     }  
  6.     return self;  

在你的ViewController中像这样写

 

 
 
  1. - (void)viewDidLoad {  
  2.     UIView *customView = [[Canvas alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];  
  3.     customView.backgroundColor = [UIColor blackColor];  
  4.     [self.view addSubview:customView];  
  5.     [customView release];  
  6.     [super viewDidLoad];  

还有,不要忘记添加 Quartz 引用:

 

 
 
  1. #import <QuartzCore/QuartzCore.h> 

我确定有很多更好的方式来做这个事,例如使用CALayers 和添加CGImage 到layers。但是那是一些我没有尝试的东西。上面的例子应该足够让你开始沿着路径做动画。

Animate along a path

这里是工程拷贝http://blog.devedup.com/wp-content/uploads/2010/06/AnimateAlongAPath.zip

xcodeProject

 

(译者水平有限,欢迎指正。)

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值