使用Core Animation对象来实现动画
转载保留原文地址:http://blog.csdn.net/kqjob/article/details/10417461,转载的
在iOS中如果使用普通的动画则可以使用UIKit提供的动画方式来实现,如果想实现更复杂的效果,则需要使用Core Animation了。
下面详解各种类型动画的使用方式
1、通过动画上下文使用UIKit动画
- -(void)animationOfUIKit
- {
- UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
- redView.backgroundColor=[UIColor redColor];
- [self.view addSubview:redView];
- //开始动画
- [UIView beginAnimations:@"test" context:nil];
- //动画时长
- [UIView setAnimationDuration:1];
- /*
- *要进行动画设置的地方
- */
- redView.backgroundColor=[UIColor blueColor];
- redView.frame=CGRectMake(50, 50, 200, 200);
- redView.alpha=0.5;
- //动画结束
- [UIView commitAnimations];
- }
2、通过代码块使用UIKit动画
- -(void)animationOfBlock
- {
- //初始化一个View,用来显示动画
- UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
- redView.backgroundColor=[UIColor redColor];
- [self.view addSubview:redView];
- [UIView animateWithDuration:1 //时长
- delay:0 //延迟时间
- options:UIViewAnimationOptionTransitionFlipFromLeft//动画效果
- animations:^{
- //动画设置区域
- redView.backgroundColor=[UIColor blueColor];
- redView.frame=CGRectMake(50, 50, 200, 200);
- redView.alpha=0.5;
- } completion:^(BOOL finish){
- //动画结束时调用
- //............
- }];
- }
使用Core Animation对象来实现动画
在Core Animation中我们经常使用的是
- CABasicAnimation
- CAKeyframeAnimation
- CATransitionAnimation
其中CABasicAnimation和CAKeyframeAnimation是对图层中的不同属性进行动画的。
如果要多整个图层进行动画,则应该使用CATransitionAnimation
如果要使用组合动画,例如要改变图层的大小和透明度,则可以先为每个属性创建一个CABasicAnimation对象,再把他们组合到CAAnimationGroup中,最后把这个组合添加到要进行动画的CALayer中。
注:CAAnimation(以及CAAnimation的子类),全部都是显式动画,这样动画播放后,表现层回恢复到模型层的原始状态,这就意味着,如果动画播放完后,会恢复到原来的样子,所以在动画播放完后要对模型层进行修改,例如:self.view.layer.backgroundColor=[UIColor blueColor].CGColor;
1、自定义动画:CABasicAnimation
- -(void)animationOfCABasicAnimation
- {
- //创建一个CABasicAnimation对象
- CABasicAnimation *animation=[CABasicAnimation animation];
- //设置颜色
- animation.toValue=(id)[UIColor blueColor].CGColor;
- //动画时间
- animation.duration=1;
- //是否反转变为原来的属性值
- animation.autoreverses=YES;
- //把animation添加到图层的layer中,便可以播放动画了。forKey指定要应用此动画的属性
- [self.view.layer addAnimation:animation forKey:@"backgroundColor"];
- }
2、关键帧动画:CAKeyframeAnimation
1. path
这是一个 CGPathRef 对象,默认是空的,当我们创建好CAKeyframeAnimation的实例的时候,可以通过制定一个自己定义的path来让 某一个物体按照这个路径进行动画。这个值默认是nil 当其被设定的时候 values 这个属性就被覆盖
2. values
一个数组,提供了一组关键帧的值, 当使用path的 时候 values的值自动被忽略。
下面是改变依次改变view的颜色
- -(void)animationOfCAKeyframeAnimation
- {
- CAKeyframeAnimation *animation=[CAKeyframeAnimation animation];
- //设置属性值
- animation.values=[NSArray arrayWithObjects:
- (id)self.view.backgroundColor,
- (id)[UIColor yellowColor].CGColor,
- (id)[UIColor greenColor].CGColor,
- (id)[UIColor blueColor].CGColor,nil];
- animation.duration=3;
- animation.autoreverses=YES;
- //把关键帧添加到layer中
- [self.view.layer addAnimation:animation forKey:@"backgroundColor"];
- }
3、使用路径制作动画:CAKeyframeAnimation
- -(void)animationOfCAKeyframeAnimationPath
- {
- //初始化一个View,用来显示动画
- UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];
- redView.backgroundColor=[UIColor redColor];
- [self.view addSubview:redView];
- CAKeyframeAnimation *ani=[CAKeyframeAnimation animation];
- //初始化路径
- CGMutablePathRef aPath=CGPathCreateMutable();
- //动画起始点
- CGPathMoveToPoint(aPath, nil, 20, 20);
- CGPathAddCurveToPoint(aPath, nil,
- 160, 30,//控制点
- 220, 220,//控制点
- 240, 380);//控制点
- ani.path=aPath;
- ani.duration=10;
- //设置为渐出
- ani.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
- //自动旋转方向
- ani.rotationMode=@"auto";
- [redView.layer addAnimation:ani forKey:@"position"];
- }
使用Core Animation对象来实现动画的更多相关文章
- iOS Core Animation学习总结(3)--动画的基本类型
一. CABasicAnimation (基础动画) 移位: CABasicAnimation *animation = [CABasicAnimation animation]; //keyPath ...
- core Animation之CAKeyframeAnimation(关键帧动画)
CABasicAnimation的区别是:CABasicAnimation只能从一个数值(fromValue)变到另一个数值(toValue),而CAKeyframeAnimation会使用一个NSA ...
- Core Animation中的组动画
实际开发中一个物体的运动往往是复合运动,单一属性的运动情况比较少,但恰恰属性动画每次进行动画设置时一次只能设置一个属性进行动画控制(不管是 基础动画还是关键帧动画都是如此),这样一来要做一个复合运动的 ...
- Core Animation之CABasicAnimation(基础动画)
#import "ViewController.h" @interface ViewController () @property(nonatomic,strong)UIButto ...
- Core Animation中的关键帧动画
键帧动画就是在动画控制过程中开发者指定主要的动画状态,至于各个状态间动画如何进行则由系统自动运算补充(每两个关键帧之间系统形成的动画称为“补间动画”),这种动画的好处就是开发者不用逐个控制每个动画帧, ...
- Core Animation中的基础动画
基础动画 在开发过程中很多情况下通过基础动画就可以满足开发需求,前面例子中使用的UIView代码块进行图像放大缩小的演示动画也是基础动画(在iOS7 中UIView也对关键帧动画进行了封装),只是UI ...
- 在ios中运用core animation暂停和继续动画
本文转载至 http://blog.csdn.net/wildfireli/article/details/23191861 暂停和继续动画的核心代码如下: <pre name="co ...
- IOS动画(Core Animation)总结 (参考多方文章)
一.简介 iOS 动画主要是指Core Animation框架.官方使用文档地址为:Core Animation Guide. Core Animation是IOS和OS X平台上负责图形渲染与动画的 ...
- iOS 动画效果:Core Animation & Facebook's pop
本文转载至 http://www.cocoachina.com/ios/20151223/14739.html 感谢原创作者分享 前言相信很多人对实现 iOS 中的动画效果都特别头疼,往往懒得动手,功 ...
随机推荐
- 图的连通性:有向图强连通分量-Tarjan算法
参考资料:http://blog.csdn.net/lezg_bkbj/article/details/11538359 上面的资料,把强连通讲的很好很清楚,值得学习. 在一个有向图G中,若两顶点间至 ...
- JavaScript原生对象总纲
一. javascript之Array类 创建js数组两种方式: var arr = []; 或var arr = new Array(); ()里可以指定长度,也可以不指定,指不指定都无所谓,因为 ...
- Linux中kettle启动spoon.sh遇到的问题
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAccAAAAYCAIAAAAAgaGrAAAE1klEQVR4nO1b2ZWrMAylLgpyPa7Gzb
- LeetCode OJ combine 3
public class Solution { public List<List<Integer>> combinationSum3(int k, int n) { retur ...
- FZU 2086 餐厅点餐
好久不写博客了……得有快一个月了……看到大神们一篇篇博文 唉……差好多……这一个月 有蓝桥杯还有校赛……校赛签到题都没签完 实力铁牌……不过蓝桥杯一等奖 五月份帝都之行还挺令人期待……清明小长假之前 ...
- redis数据类型:sorted sets类型及操作
sorted sets类型及操作: sorted set是set的一个升级版本,它是在set的基础上增加了一个顺序 属性,这一属性在添加修改元素的时候可以指定,每次指定后,zset会 自动重新按新的值 ...
- PHP实现中文截取无乱码
字符串的处理是编程中比较常见的,各种编程语言对字符串的处理也提供了大量函数,像php中mb_substr()函数可以实现对中文字符串的截取,如何使用自定义方法实现中文字符串截取无乱码这也是面试经常遇到 ...
- hibernate的批量更新、批量删除
hibernate的批处理API:session.createQuery(hql).executeUpdate(),如果有参数则在执行之前设置参数. 批量更新示例: @Test public void ...
- codeforce Gym 101102A Coins (01背包变形)
01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...
- android 线程池的使用
转自http://www.trinea.cn/android/java-android-thread-pool/ Java(Android)线程池 介绍new Thread的弊端及Java四种线程池的 ...