博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iPhone: 对象沿路径动画
阅读量:7222 次
发布时间:2019-06-29

本文共 4698 字,大约阅读时间需要 15 分钟。

 

这里我将展示如何在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

这里是工程拷贝

 

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

本文转自 arthurchen 51CTO博客,原文链接:http://blog.51cto.com/arthurchen/641572,如需转载请自行联系原作者
你可能感兴趣的文章
秋色园QBlog技术原理解析:Module之页面基类-生命周期流程(六)
查看>>
争议中的高通,以及外界对它的三个认知误区
查看>>
Netflix或将退出支持网络中立原则联盟
查看>>
数据分析的5个坑,你踩过几个?
查看>>
“智慧城市”建设之道
查看>>
用友 “企业互联网服务” 生态圈模式“打天下”
查看>>
社交媒体对零售业影响越来越大 值得密切关注
查看>>
加大打击防护 江苏移动织就立体信息安全网
查看>>
同有全闪存阵列设计新思路:一硬到底!
查看>>
从欧洲NB-IOT大战窥物联网竞争焦点
查看>>
开车不低头 车里子智能轻车机C1淘宝众筹中
查看>>
海云数据CEO冯一村:可视分析是变现大数据价值最终一环
查看>>
sql注入攻击
查看>>
上海今年拟增光伏装机约136MW
查看>>
推动建设智慧可持续的城市
查看>>
《Puppet权威指南》——3.1 Puppet 各环境的安装
查看>>
服务器维护之除尘篇
查看>>
音乐行业如何运用大数据打造下一个“大热门”?
查看>>
安防产业收购并购 是资本游戏还是资源整合?
查看>>
一张表有且只有一条记录(续) - 支持插入,并且查询、更新、删除只作用在最后一条记录上...
查看>>