Core Animation之多种动画效果

前面介绍了Core Animation基础知识,还有CALayer的简单使用,最终还是有要动画的滴,这里列出几个动画效果,参考下能加深对Core Animation的认识和理解。
 
1、把图片移到右下角变小透明
使用CAAnimationGroup叠加动画效果,就是下面按钮《把图片移到右下角变小透明》描述的效果:
上面三个图是动画的三个状态,实现代码如下:
 
 
 
  1. - (void)viewDidLoad 
  2.     [super viewDidLoad]; 
  3.     self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"snaguosha.png"]]; 
  4.     self.imageView.frame = CGRectMake(10, 10, 128, 192); 
  5.     [self.view addSubview:self.imageView]; 
  6.  } 
 
 
 
  1. - (IBAction)tranAction:(id)sender { 
  2.     CGPoint fromPoint = self.imageView.center; 
  3.      
  4.     //路径曲线 
  5.     UIBezierPath *movePath = [UIBezierPath bezierPath]; 
  6.     [movePath moveToPoint:fromPoint]; 
  7.     CGPoint toPoint = CGPointMake(300, 460); 
  8.     [movePath addQuadCurveToPoint:toPoint 
  9.                      controlPoint:CGPointMake(300,0)]; 
  10.     //关键帧 
  11.     CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
  12.     moveAnim.path = movePath.CGPath; 
  13.     moveAnim.removedOnCompletion = YES; 
  14.      
  15.     //旋转变化 
  16.     CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"]; 
  17.     scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; 
  18.     //x,y轴缩小到0.1,Z 轴不变 
  19.     scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]; 
  20.     scaleAnim.removedOnCompletion = YES; 
  21.      
  22.     //透明度变化 
  23.     CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"alpha"]; 
  24.     opacityAnim.fromValue = [NSNumber numberWithFloat:1.0]; 
  25.     opacityAnim.toValue = [NSNumber numberWithFloat:0.1]; 
  26.     opacityAnim.removedOnCompletion = YES; 
  27.      
  28.     //关键帧,旋转,透明度组合起来执行 
  29.     CAAnimationGroup *animGroup = [CAAnimationGroup animation]; 
  30.     animGroup.animations = [NSArray arrayWithObjects:moveAnim, scaleAnim,opacityAnim, nil]; 
  31.     animGroup.duration = 1; 
  32.     [self.imageView.layer addAnimation:animGroup forKey:nil]; 
代码解析:上面关键帧设置了动画的路径,scaleAnim设置了缩小,opacityAnim设置了透明度的变化。把三个动画组合到:animGroup。
 
在把animGroup添加到imageView.layer层的动画里。于是动画效果就有了。
 
2、旋转并向右移动
 
 
  1. - (IBAction)RightRotateAction:(id)sender { 
  2.     CGPoint fromPoint = self.imageView.center; 
  3.     UIBezierPath *movePath = [UIBezierPath bezierPath]; 
  4.     [movePath moveToPoint:fromPoint]; 
  5.     CGPoint toPoint = CGPointMake(fromPoint.x +100 , fromPoint.y ) ; 
  6.     [movePath addLineToPoint:toPoint]; 
  7.      
  8.     CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
  9.     moveAnim.path = movePath.CGPath; 
  10.      
  11.     CABasicAnimation *TransformAnim = [CABasicAnimation animationWithKeyPath:@"transform"]; 
  12.     TransformAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; 
  13.      
  14.     //沿Z轴旋转 
  15.     TransformAnim.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI,0,0,1)]; 
  16.      
  17.     //沿Y轴旋转 
  18.   //   scaleAnim.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI,0,1.0,0)]; 
  19.      
  20.     //沿X轴旋转 
  21. //     TransformAnim.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI,1.0,0,0)]; 
  22.     TransformAnim.cumulative = YES; 
  23.     TransformAnim.duration =3; 
  24.     //旋转2遍,360度 
  25.     TransformAnim.repeatCount =2; 
  26.     self.imageView.center = toPoint; 
  27.     TransformAnim.removedOnCompletion = YES; 
  28.     CAAnimationGroup *animGroup = [CAAnimationGroup animation]; 
  29.     animGroup.animations = [NSArray arrayWithObjects:moveAnim, TransformAnim, nil]; 
  30.     animGroup.duration = 6; 
  31.      
  32.     [self.imageView.layer addAnimation:animGroup forKey:nil]; 
代码解析:可能你没注意到,CATransform3DMakeRotation,这返回的是旋转的值。上面的动画效果里返回的是CATransform3DMakeScale缩放的值。
 
向右移动是因为关键帧使用了路径为直线的路径。
 
3、旋转并消除边缘锯齿
 
 
 
  1. - (IBAction)Rotate360Action:(id)sender { 
  2.     //图片旋转360度 
  3.     CABasicAnimation *animation = [ CABasicAnimation 
  4.                                    animationWithKeyPath: @"transform" ]; 
  5.     animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; 
  6.      
  7.     //围绕Z轴旋转,垂直与屏幕 
  8.     animation.toValue = [ NSValue valueWithCATransform3D: 
  9.                          CATransform3DMakeRotation(M_PI, 0, 0, 1.0) ]; 
  10.     animation.duration = 3; 
  11.     //旋转效果累计,先转180度,接着再旋转180度,从而实现360旋转 
  12.     animation.cumulative = YES; 
  13.     animation.repeatCount = 2; 
  14.      
  15.     //在图片边缘添加一个像素的透明区域,去图片锯齿 
  16.     CGRect imageRrect = CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height); 
  17.     UIGraphicsBeginImageContext(imageRrect.size); 
  18.     [self.imageView.image drawInRect:CGRectMake(1,1,self.imageView.frame.size.width-2,self.imageView.frame.size.height-2)]; 
  19.     self.imageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
  20.     UIGraphicsEndImageContext(); 
  21.      
  22.     [self.imageView.layer addAnimation:animation forKey:nil]; 
如果你仔细观察,会看到第二个动画里在旋转时,图片边缘是有锯齿的,如何消除呢?在图片边缘添加一个像素的透明区域,去图片锯齿。
 
UIGraphicsBeginImageContext 开始图片内容。
UIGraphicsGetImageFromCurrentImageContext 获取当前内容作为图片。
UIGraphicsEndImageContext结束。是和UIGraphicsBeginImageContext配套使用的。
 
4、吃豆人动画
这个有点复杂,首先说下实现的步骤:
画一个吃豆人开口的路径:pacmanOpenPath
画一个吃豆人闭口的路径:pacmanClosedPath
新建一个闭口的吃豆人头的层:shapeLayer
 
把开口和闭口路径设置成CABasicAnimation *chompAnimation动画的起点和终点,这样循环就能出现咬牙的动画了。
 
最后设置一个路径为关键帧,让吃豆人在这条路径上行动。
 
代码如下:
 
 
  1. - (void)animationInit 
  2.     self.view.backgroundColor = [UIColor blackColor]; 
  3.      
  4.     CGFloat radius = 30.0f; 
  5.     CGFloat diameter = radius * 2; 
  6.     CGPoint arcCenter = CGPointMake(radius, radius); 
  7.     // Create a UIBezierPath for Pacman's open state 
  8.     pacmanOpenPath = [UIBezierPath bezierPathWithArcCenter:arcCenter 
  9.                                                     radius:radius 
  10.                                                 startAngle:DEGREES_TO_RADIANS(35) 
  11.                                                   endAngle:DEGREES_TO_RADIANS(315) 
  12.                                                  clockwise:YES]; 
  13.      
  14.     [pacmanOpenPath addLineToPoint:arcCenter]; 
  15.     [pacmanOpenPath closePath]; 
  16.      
  17.     // Create a UIBezierPath for Pacman's close state 
  18.     pacmanClosedPath = [UIBezierPath bezierPathWithArcCenter:arcCenter 
  19.                                                       radius:radius 
  20.                                                   startAngle:DEGREES_TO_RADIANS(1) 
  21.                                                     endAngle:DEGREES_TO_RADIANS(359) 
  22.                                                    clockwise:YES]; 
  23.     [pacmanClosedPath addLineToPoint:arcCenter]; 
  24.     [pacmanClosedPath closePath]; 
  25.      
  26.     // Create a CAShapeLayer for Pacman, fill with yellow 
  27.     shapeLayer = [CAShapeLayer layer]; 
  28.     shapeLayer.fillColor = [UIColor yellowColor].CGColor; 
  29.     shapeLayer.path = pacmanClosedPath.CGPath; 
  30.     shapeLayer.strokeColor = [UIColor grayColor].CGColor; 
  31.     shapeLayer.lineWidth = 1.0f; 
  32.     shapeLayer.bounds = CGRectMake(0, 0, diameter, diameter); 
  33.     shapeLayer.position = CGPointMake(-40, -100); 
  34.     [self.view.layer addSublayer:shapeLayer]; 
  35.      
  36.     SEL startSelector = @selector(startAnimation); 
  37.     UIGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:startSelector]; 
  38.     [self.view addGestureRecognizer:recognizer]; 
 
 
 
  1. - (void)startAnimation { 
  2.     // 创建咬牙动画 
  3.     CABasicAnimation *chompAnimation = [CABasicAnimation animationWithKeyPath:@"path"]; 
  4.     chompAnimation.duration = 0.25; 
  5.     chompAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
  6.     chompAnimation.repeatCount = HUGE_VALF; 
  7.     chompAnimation.autoreverses = YES; 
  8.     // Animate between the two path values 
  9.     chompAnimation.fromValue = (id)pacmanClosedPath.CGPath; 
  10.     chompAnimation.toValue = (id)pacmanOpenPath.CGPath; 
  11.     [shapeLayer addAnimation:chompAnimation forKey:@"chompAnimation"]; 
  12.      
  13.     // Create digital '2'-shaped path 
  14.      
  15.     UIBezierPath *path = [UIBezierPath bezierPath]; 
  16.     [path moveToPoint:CGPointMake(0, 100)]; 
  17.     [path addLineToPoint:CGPointMake(300, 100)]; 
  18.     [path addLineToPoint:CGPointMake(300, 200)]; 
  19.     [path addLineToPoint:CGPointMake(0, 200)]; 
  20.     [path addLineToPoint:CGPointMake(0, 300)]; 
  21.     [path addLineToPoint:CGPointMake(300, 300)]; 
  22.      
  23.     CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
  24.     moveAnimation.path = path.CGPath; 
  25.     moveAnimation.duration = 8.0f; 
  26.     // Setting the rotation mode ensures Pacman's mouth is always forward.  This is a very convenient CA feature. 
  27.     moveAnimation.rotationMode = kCAAnimationRotateAuto; 
  28.     [shapeLayer addAnimation:moveAnimation forKey:@"moveAnimation"]; 
 
 
 
  1. - (void)viewDidLoad 
  2.     [super viewDidLoad]; 
  3.     [self animationInit]; 
还需要添加一个宏:
#define DEGREES_TO_RADIANS(x) (3.14159265358979323846 * x / 180.0)    计算角度转换
 
添加了个手势,点一下屏幕,吃豆人就动起来了。效果:
本篇例子代码: 代码
 
来源:荣芳志的博客
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
辽B代驾管理系统对代驾订单管理、用户咨询管理、代驾订单评价管理、代驾订单投诉管理、字典管理、论坛管理、公告管理、新闻信息管理、司机管理、用户管理、管理员管理等进行集中化处理。经过前面自己查阅的网络知识,加上自己在学校课堂上学习的知识,决定开发系统选择小程序模式这种高效率的模式完成系统功能开发。这种模式让操作员基于浏览器的方式进行网站访问,采用的主流的Java语言这种面向对象的语言进行辽B代驾管理系统程序的开发,在数据库的选择上面,选择功能强大的Mysql数据库进行数据的存放操作。辽B代驾管理系统的开发让用户查看代驾订单信息变得容易,让管理员高效管理代驾订单信息。 辽B代驾管理系统具有管理员角色,用户角色,这几个操作权限。 辽B代驾管理系统针对管理员设置的功能有:添加并管理各种类型信息,管理用户账户信息,管理代驾订单信息,管理公告信息等内容。 辽B代驾管理系统针对用户设置的功能有:查看并修改个人信息,查看代驾订单信息,查看公告信息等内容。 辽B代驾管理系统针对管理员设置的功能有:添加并管理各种类型信息,管理用户账户信息,管理代驾订单信息,管理公告信息等内容。 辽B代驾管理系统针对用户设置的功能有:查看并修改个人信息,查看代驾订单信息,查看公告信息等内容。 系统登录功能是程序必不可少的功能,在登录页面必填的数据有两项,一项就是账号,另一项数据就是密码,当管理员正确填写并提交这二者数据之后,管理员就可以进入系统后台功能操作区。项目管理页面提供的功能操作有:查看代驾订单,删除代驾订单操作,新增代驾订单操作,修改代驾订单操作。公告信息管理页面提供的功能操作有:新增公告,修改公告,删除公告操作。公告类型管理页面显示所有公告类型,在此页面既可以让管理员添加新的公告信息类型,也能对已有的公告类型信息执行编辑更新,失效的公告类型信息也能让管理员快速删除。新闻管理页面,此页面提供给管理员的功能有:新增新闻,修改新闻,删除新闻。新闻类型管理页面,此页面提供给管理员的功能有:新增新闻类型,修改新闻类型,删除新闻类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值