ios之CABasicAnimation
博主:最近iOS开发中用到CoreAnimation的framework来做动画效果,虽然以前也用过,但一直没有系统学习过,今天看到一篇非常详细的博文(虽然是日语,但真的写的很好),在此翻译出来供大家学习。
原帖地址:http://www.objectivec-iphone.com/animation/CoreAnimation/CABasicAnimation.html
本文为博主翻译,若需转载,请注明出处:http://blog.csdn.net/iosevanhuang/article/details/14488239
CABasicAnimation类的使用方式就是基本的关键帧动画。
所谓关键帧动画,就是将Layer的属性作为KeyPath来注册,指定动画的起始帧和结束帧,然后自动计算和实现中间的过渡动画的一种动画方式。
CABasicAnimation的基本使用顺序
1.引用QuartzCore.framework
将"QuartzCore.framework"这个库添加到项目中。并且在需要使用CABaseAnimation类的地方import头文件。
- #import <QuartzCore/QuartzCore.h>
2.CABaseAnimation的实例化以及关键路径的注册
使用"animationWithKeyPath:"方法进行CABasicAnimation的实例化,并指定Layer的属性作为关键路径来注册。
- // 指定position属性
- CABasicAnimation *animation =
- [CABasicAnimation animationWithKeyPath:@"position"];
3.设定动画
设定动画的属性。以下是属性及其对应的说明:
属性 | 说明 |
---|---|
duration | 动画时长(秒为单位)(注:此处与原文有出入) |
repeatCount | 重复次数。永久重复的话设置为HUGE_VALF。 |
beginTime | 指定动画开始时间。从开始指定延迟几秒执行的话,请设置为 「CACurrentMediaTime() + 秒数」的形式。 |
timingFunction | 设定动画的速度变化 |
autoreverses | 动画结束时是否执行逆动画 |
例:
- animation.duration = 2.5; // 动画持续时间
- animation.repeatCount = 1; // 不重复
- animation.beginTime = CACurrentMediaTime() + 2; // 2秒后执行
- animation.autoreverses = YES; // 结束后执行逆动画
- // 动画先加速后减速
- animation.timingFunction =
- [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];
4.设定动画的开始帧和结束帧
设定动画开始和结束帧时的状态。设定的值会变为KeyPath所指定的属性的值。
属性 | 说明 |
---|---|
fromValue | 开始值 |
toValue | 终了值(絶対値) |
byValue | 终了值(相对值) |
例:
- // 指定position属性(移动)
- CABasicAnimation *animation =
- [CABasicAnimation animationWithKeyPath:@"position"];
- ・・・
- // 设定动画起始帧和结束帧
- animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)]; // 起始点
- animation.toValue = [NSValue valueWithCGPoint:CGPointMake(320, 480)]; // 终了点
5.添加动画
为Layer添加设置完成的动画,可以给Key指定任意名字。
- [myView.layer addAnimation:animation forKey:@"move-layer"];
其他.动画结束后回到初始状态的现象的解决方法
用CABasicAnimation执行动画,在动画结束后会回归动画开始前的状态。想要解决的话,必须设置“removedOnCompletion”和“fillMode”这两个属性。
- // 动画终了后不返回初始状态
- animation.removedOnCompletion = NO;
- animation.fillMode = kCAFillModeForwards;
CABasicAnimation的使用示例
实际上CABasicAnimation有很多种使用方法,以下将一一列举。
移动动画
- /* 移动 */
- CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
- // 动画选项的设定
- animation.duration = 2.5; // 持续时间
- animation.repeatCount = 1; // 重复次数
- // 起始帧和终了帧的设定
- animation.fromValue = [NSValue valueWithCGPoint:myView.layer.position]; // 起始帧
- animation.toValue = [NSValue valueWithCGPoint:CGPointMake(320, 480)]; // 终了帧
- // 添加动画
- [myView.layer addAnimation:animation forKey:@"move-layer"];
旋转动画
- /* 旋转 */
- // 对Y轴进行旋转(指定Z轴的话,就和UIView的动画一样绕中心旋转)
- CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
- // 设定动画选项
- animation.duration = 2.5; // 持续时间
- animation.repeatCount = 1; // 重复次数
- // 设定旋转角度
- animation.fromValue = [NSNumber numberWithFloat:0.0]; // 起始角度
- animation.toValue = [NSNumber numberWithFloat:22 * M_PI]; // 终止角度
- // 添加动画
- [myView.layer addAnimation:animation forKey:@"rotate-layer"];
缩放动画
- /* 放大缩小 */
- // 设定为缩放
- CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
- // 动画选项设定
- animation.duration = 2.5; // 动画持续时间
- animation.repeatCount = 1; // 重复次数
- animation.autoreverses = YES; // 动画结束时执行逆动画
- // 缩放倍数
- animation.fromValue = [NSNumber numberWithFloat:1.0]; // 开始时的倍率
- animation.toValue = [NSNumber numberWithFloat:2.0]; // 结束时的倍率
- // 添加动画
- [myView.layer addAnimation:animation forKey:@"scale-layer"];
组合动画
使用CAAnimationGroup类进行复数动画的组合。代码如下:
- /* 动画1(在X轴方向移动) */
- CABasicAnimation *animation1 =
- [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
- // 终点设定
- animation1.toValue = [NSNumber numberWithFloat:80];; // 終点
- /* 动画2(绕Z轴中心旋转) */
- CABasicAnimation *animation2 =
- [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
- // 设定旋转角度
- animation2.fromValue = [NSNumber numberWithFloat:0.0]; // 开始时的角度
- animation2.toValue = [NSNumber numberWithFloat:44 * M_PI]; // 结束时的角度
- /* 动画组 */
- CAAnimationGroup *group = [CAAnimationGroup animation];
- // 动画选项设定
- group.duration = 3.0;
- group.repeatCount = 1;
- // 添加动画
- group.animations = [NSArray arrayWithObjects:animation1, animation2, nil nil];
- [myView.layer addAnimation:group forKey:@"move-rotate-layer"];
捕获动画开始时和终了时的事件
博主:设定委托对象,实现委托方法,如下:
- /* 移动 */
- CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
- animation.delegate = self; // 指定委托对象
- // 设定动画选项
- animation.duration = 2.5; // 动画时长
- animation.repeatCount = 1; // 重复次数
- // 终点设定
- animation.toValue = [NSNumber numberWithFloat:100];; // 终点
- // 添加动画
- [myView.layer addAnimation:animation forKey:@"move-layer"];
- ・・・
- /**
- * 动画开始时
- */
- - (void)animationDidStart:(CAAnimation *)theAnimation
- {
- NSLog(@"begin");
- }
- /**
- * 动画结束时
- */
- - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
- {
- NSLog(@"end");
- }
CABasicAnimation使用时候的注意点
CABasicAnimation正在进行动画的时候,点击了Home按钮后再回到app的时候,动画会被清空。
Objective-C的示例程序
使用CABasicAnimation实现关键帧动画的示例程序如下:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // 图像显示
- UIImage *image = [UIImage imageNamed:@"image01.png"];
- UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
- imageView.center = CGPointMake(160, 240);
- [self.view addSubview:imageView];
- /* 移动 */
- CABasicAnimation *animation1 =
- [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
- // 起止点的设定
- animation1.toValue = [NSNumber numberWithFloat:100];; // 終点
- /* 旋转 */
- CABasicAnimation *animation2 =
- [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
- // 绕x轴转3圈
- animation2.toValue = [NSNumber numberWithFloat:66 * M_PI]; // 结束时的角度
- /* 动画组 */
- CAAnimationGroup *group = [CAAnimationGroup animation];
- group.delegate = self;
- group.duration = 5.0;
- group.repeatCount = 1;
- // 动画结束后不变回初始状态
- group.removedOnCompletion = NO;
- group.fillMode = kCAFillModeForwards;
- // 添加动画
- group.animations = [NSArray arrayWithObjects:animation1, animation2, nil nil];
- [imageView.layer addAnimation:group forKey:@"move-rotate-layer"];
- }
- /**
- * 动画开始时
- */
- - (void)animationDidStart:(CAAnimation *)theAnimation
- {
- NSLog(@"begin");
- }
- /**
- * 动画结束时
- */
- - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
- {
- NSLog(@"end");
- }
示例程序的执行结果
Objective-C的示例程序的执行结果如下:
控制台输出如下:
- begin
- end
ios之CABasicAnimation的更多相关文章
- iOS开发CABasicAnimation动画理解
1.CALayer简介 CALayer是个与UIView很类似的概念,同样有backgroundColor.frame等相似的属性,我们可以将UIView看做一种特殊的CALayer.但实际上UIVi ...
- iOS Layer CABasicAnimation
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- iOS - CABasicAnimation
代码实例: [1] - (void)pulseClick { //!> 宽和高等比例转换 CABasicAnimation * pulse = [CABasicAnimation animati ...
- IOS第18天(5,CABasicAnimation基本动画)
******* #import "HMViewController.h" @interface HMViewController () @property (nonatomic, ...
- iOS:核心动画之基本动画CABasicAnimation
基本动画,是CAPropertyAnimation的子类 属性说明: fromValue:keyPath相应属性的初始值 toValue:keyPath相应属性的结束值 动画过程说明: 随着动画的进行 ...
- iOS开发——动画编程Swift篇&(四)CABasicAnimation动画
CABasicAnimation动画 //CABasicAnimation-不透明度 @IBAction func cabOpacity() { let animation = CABasicAnim ...
- CABasicAnimation 基本动画 分类: ios技术 2015-07-16 17:10 132人阅读 评论(0) 收藏
几个可以用来实现热门APP应用PATH中menu效果的几个方法 +(CABasicAnimation *)opacityForever_Animation:(float)time //永久闪烁的动画 ...
- iOS核心动画详解(CABasicAnimation)
前言 上一篇已经介绍了核心动画在UI渲染中的位置和基本概念,但是没有具体介绍CAAnimation子类的用法,本文将介绍CABasicAnimation及其子类CASpringAnimation的用法 ...
- AJ学IOS(39)UI之核心动画之CABasicAnimation(基础动画)
AJ分享,必须精品 一.CABasicAnimation简介 CAPropertyAnimation的子类 属性解析: fromValue:keyPath相应属性的初始值 toValue:keyPat ...
随机推荐
- Ocelot + IdentityServer4 构建 GateWay
上一篇已经构建好了例子,接下来将IdentityServer4添加到Ocelot中去实现 配置一个客户端配置,可以构建一个简单的客户端信息,这里我用的混合模式,配置比较多,对于客户端模式而言实际很多都 ...
- day12--python操作mysql
本篇对于Python操作MySQL主要使用两种方式: 1.原生模块 pymsql(http://www.cnblogs.com/wupeiqi/articles/5713330.htm ...
- net mvc中angular
把angular项目整合到.net mvc中 之前的开发选择的是完全舍弃服务端,仅保留最简单web服务器提供angular经打包的静态资源,此外所有的业务与数据请求都访问一个分离的WebApi来实 ...
- 【LOJ】#2509. 「AHOI / HNOI2018」排列
题解 虽然要求一个dfs序,但是不是从根开始贪心 从最小的点开始贪心,最小的点显然是父亲选了之后马上就选它 那么我们每次把最小的点和父亲合并,两个联通块之间也是如此 对于两个联通块,他们合并的顺序应该 ...
- Codeforces 291 E Tree-String Problem AC自动机
Tree-String Problem 网上的dfs + kmp 复杂度就是错的, 除非算出根据下一个字符直接转移Next数组直接转移, 而求出Next[ i ][ 26 ]数组和丢进AC自动机里面没 ...
- streaming优化:禁用序列化
如果你的streaming处理数据的时间间隔比较小,并且没有窗口操作,那么可以考虑不使用序列化,这样可以减少内存和cpu的使用,加快数据处理效率
- 【Java】 大话数据结构(8) 串的模式匹配算法(朴素、KMP、改进算法)
本文根据<大话数据结构>一书,实现了Java版的串的朴素模式匹配算法.KMP模式匹配算法.KMP模式匹配算法的改进算法. 1.朴素的模式匹配算法 为主串和子串分别定义指针i,j. (1)当 ...
- PAT 1066. Root of AVL Tree (25)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- 003.LVM扩容
一 LVM扩容步骤 创建分区 创建PV 扩容VG 扩容LV 载大小 二 创建分区 使用分区工具(如fdisk等)创建LVM分区,却将分区标识为LVM的分区类型8e. [root@kauai ~]# f ...
- UI自动化测试(五)TestNG简介与安装步骤
简述 TestNG是一个设计用来简化广泛的测试需求的测试框架, 从单元测试(隔离测试一个类) 到集成测试(测试由有多个类多个包甚至多个外部框架组成的整个系统, 例如运用服务器) . testNG灵感来 ...