IOS动画Core Animation详解

转载保留原文地址:http://blog.csdn.net/kqjob/article/details/10417461

在IOS中如果使用普通的动画则可以使用UIKit提供的动画方式来实现,如果想实现更复杂的效果,则需要使用Core Animation了。

下面详解各种类型动画的使用方式

1、通过动画上下文使用UIKit动画

[plain]  view plain copy
  1. -(void)animationOfUIKit  
  2. {  
  3.     UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];  
  4.     redView.backgroundColor=[UIColor redColor];  
  5.       
  6.     [self.view addSubview:redView];  
  7.     //开始动画  
  8.     [UIView beginAnimations:@"test" context:nil];  
  9.     //动画时长  
  10.     [UIView setAnimationDuration:1];  
  11.     /*  
  12.      *要进行动画设置的地方  
  13.      */  
  14.       
  15.     redView.backgroundColor=[UIColor blueColor];  
  16.     redView.frame=CGRectMake(50, 50, 200, 200);  
  17.     redView.alpha=0.5;  
  18.       
  19.       
  20.     //动画结束  
  21.     [UIView commitAnimations];  
  22. }  


2、通过代码块使用UIKit动画

[plain]  view plain copy
  1. -(void)animationOfBlock  
  2. {  
  3.     //初始化一个View,用来显示动画  
  4.     UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];  
  5.     redView.backgroundColor=[UIColor redColor];  
  6.       
  7.     [self.view addSubview:redView];  
  8.   
  9.     [UIView animateWithDuration:1 //时长  
  10.                           delay:0 //延迟时间  
  11.                         options:UIViewAnimationOptionTransitionFlipFromLeft//动画效果  
  12.                      animations:^{  
  13.                            
  14.                          //动画设置区域  
  15.                          redView.backgroundColor=[UIColor blueColor];  
  16.                          redView.frame=CGRectMake(50, 50, 200, 200);  
  17.                          redView.alpha=0.5;  
  18.                            
  19.                      } completion:^(BOOL finish){  
  20.                        //动画结束时调用  
  21.                        //............  
  22.                      }];  
  23.       
  24.       
  25. }  

使用Core Animation对象来实现动画


在Core Animation中我们经常使用的是

  • CABasicAnimation
  • CAKeyframeAnimation
  • CATransitionAnimation

其中CABasicAnimationCAKeyframeAnimation是对图层中的不同属性进行动画的。

如果要多整个图层进行动画,则应该使用CATransitionAnimation

如果要使用组合动画,例如要改变图层的大小和透明度,则可以先为每个属性创建一个CABasicAnimation对象,再把他们组合到CAAnimationGroup中,最后把这个组合添加到要进行动画的CALayer中。

注:CAAnimation(以及CAAnimation的子类),全部都是显式动画,这样动画播放后,表现层回恢复到模型层的原始状态,这就意味着,如果动画播放完后,会恢复到原来的样子,所以在动画播放完后要对模型层进行修改,例如:self.view.layer.backgroundColor=[UIColor blueColor].CGColor;

1、自定义动画:CABasicAnimation

[plain]  view plain copy
  1. -(void)animationOfCABasicAnimation  
  2. {  
  3.     //创建一个CABasicAnimation对象  
  4.     CABasicAnimation *animation=[CABasicAnimation animation];  
  5.     //设置颜色  
  6.     animation.toValue=(id)[UIColor blueColor].CGColor;  
  7.     //动画时间  
  8.     animation.duration=1;  
  9.     //是否反转变为原来的属性值  
  10.     animation.autoreverses=YES;  
  11.     //把animation添加到图层的layer中,便可以播放动画了。forKey指定要应用此动画的属性  
  12.     [self.view.layer addAnimation:animation forKey:@"backgroundColor"];  
  13.       
  14. }  


2、关键帧动画:CAKeyframeAnimation

1. path

这是一个 CGPathRef  对象,默认是空的,当我们创建好CAKeyframeAnimation的实例的时候,可以通过制定一个自己定义的path来让  某一个物体按照这个路径进行动画。这个值默认是nil  当其被设定的时候  values  这个属性就被覆盖 

2. values

一个数组,提供了一组关键帧的值,  当使用path的 时候 values的值自动被忽略。

下面是改变依次改变view的颜色

[plain]  view plain copy
  1. -(void)animationOfCAKeyframeAnimation  
  2. {  
  3.     CAKeyframeAnimation *animation=[CAKeyframeAnimation animation];  
  4.     //设置属性值  
  5.     animation.values=[NSArray arrayWithObjects:  
  6.                       (id)self.view.backgroundColor,  
  7.                       (id)[UIColor yellowColor].CGColor,  
  8.                       (id)[UIColor greenColor].CGColor,  
  9.                       (id)[UIColor blueColor].CGColor,nil];  
  10.     animation.duration=3;  
  11.     animation.autoreverses=YES;  
  12.     //把关键帧添加到layer中  
  13.     [self.view.layer addAnimation:animation forKey:@"backgroundColor"];  
  14. }  


3、使用路径制作动画:CAKeyframeAnimation

[plain]  view plain copy
  1. -(void)animationOfCAKeyframeAnimationPath  
  2. {  
  3.     //初始化一个View,用来显示动画  
  4.     UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];  
  5.     redView.backgroundColor=[UIColor redColor];  
  6.       
  7.     [self.view addSubview:redView];  
  8.       
  9.     CAKeyframeAnimation *ani=[CAKeyframeAnimation animation];  
  10.     //初始化路径  
  11.     CGMutablePathRef aPath=CGPathCreateMutable();  
  12.     //动画起始点  
  13.     CGPathMoveToPoint(aPath, nil, 20, 20);  
  14.     CGPathAddCurveToPoint(aPath, nil,   
  15.                           160, 30,//控制点  
  16.                           220, 220,//控制点   
  17.                           240, 380);//控制点  
  18.       
  19.     ani.path=aPath;  
  20.     ani.duration=10;  
  21.     //设置为渐出  
  22.     ani.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];  
  23.     //自动旋转方向  
  24.     ani.rotationMode=@"auto";  
  25.       
  26.     [redView.layer addAnimation:ani forKey:@"position"];  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单片微型计算机(MCU)经过多年的发展,在性能上有很大的进步,在型号上发展到上千种类,已经广泛应用于人类社会生活的各个领域。单片机课程已经成为高校计算机、自动化、测控以及电子信息工程等专业的重要课程。该课程是一门理论性和实践性都很强的课程,在实际教学中,应将理论教学和实验教学紧密结合。学生在掌握理论知识之余,必须通过编写程序、设计硬件电路、仿真、调试这一系列的实验过程,才能更好地掌握单片机的结构原理和应用技能。随着单片机及其接口技术的飞速发展,目前市场上供应的编程仿真实验资源并不能完全满足高校单片机课程教与学的需求,构建低成本、技术先进、源码公开的单片机编程仿真实验系统,对我国单片机课程的教学和单片机领域人才的培养具有重要的现实意义。 本论文结合目前教学中对单片机编程仿真实验系统的实际需求,采用模块化结构设计思想,精心设计和开发了单片机编程仿真实验系统。该单片机编程仿真实验系统由PC机端单片机编程控制软件和单片机编程仿真实验板两部分组成。PC机端的单片机编程控制软件可以自动检测到连接到单片机编程仿真实验板上的单片机,控制单片机编程器擦除、写入、读出、校验目标单片机ROM中的程序,以十六进制文件(.HEX文件)格式显示在控制界面内;单片机仿真实验系统能够把写入单片机的程序实时地运行,并呈现实际运行效果。单片机编程控制软件和单片机仿真实验板组成一个完整的单片机编程仿真实验系统。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值