博主:最近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头文件。

  1. #import <QuartzCore/QuartzCore.h>

2.CABaseAnimation的实例化以及关键路径的注册

使用"animationWithKeyPath:"方法进行CABasicAnimation的实例化,并指定Layer的属性作为关键路径来注册。

  1. // 指定position属性
  2. CABasicAnimation *animation =
  3. [CABasicAnimation animationWithKeyPath:@"position"];

3.设定动画

设定动画的属性。以下是属性及其对应的说明:

属性 说明
duration 动画时长(秒为单位)(注:此处与原文有出入)
repeatCount 重复次数。永久重复的话设置为HUGE_VALF。
beginTime 指定动画开始时间。从开始指定延迟几秒执行的话,请设置为
「CACurrentMediaTime() + 秒数」的形式。
timingFunction 设定动画的速度变化
autoreverses 动画结束时是否执行逆动画

例:

  1. animation.duration = 2.5; // 动画持续时间
  2. animation.repeatCount = 1; // 不重复
  3. animation.beginTime = CACurrentMediaTime() + 2; // 2秒后执行
  4. animation.autoreverses = YES; // 结束后执行逆动画
  5. // 动画先加速后减速
  6. animation.timingFunction =
  7. [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];

4.设定动画的开始帧和结束帧

设定动画开始和结束帧时的状态。设定的值会变为KeyPath所指定的属性的值。

属性 说明
fromValue 开始值
toValue 终了值(絶対値)
byValue 终了值(相对值)

例:

  1. // 指定position属性(移动)
  2. CABasicAnimation *animation =
  3. [CABasicAnimation animationWithKeyPath:@"position"];
  4. ・・・
  5. // 设定动画起始帧和结束帧
  6. animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)]; // 起始点
  7. animation.toValue = [NSValue valueWithCGPoint:CGPointMake(320, 480)]; // 终了点

5.添加动画

为Layer添加设置完成的动画,可以给Key指定任意名字。

  1. [myView.layer addAnimation:animation forKey:@"move-layer"];

其他.动画结束后回到初始状态的现象的解决方法

用CABasicAnimation执行动画,在动画结束后会回归动画开始前的状态。想要解决的话,必须设置“removedOnCompletion”和“fillMode”这两个属性。

  1. // 动画终了后不返回初始状态
  2. animation.removedOnCompletion = NO;
  3. animation.fillMode = kCAFillModeForwards;

CABasicAnimation的使用示例

实际上CABasicAnimation有很多种使用方法,以下将一一列举。

移动动画

移动动画的代码如下:
  1. /* 移动 */
  2. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
  3. // 动画选项的设定
  4. animation.duration = 2.5; // 持续时间
  5. animation.repeatCount = 1; // 重复次数
  6. // 起始帧和终了帧的设定
  7. animation.fromValue = [NSValue valueWithCGPoint:myView.layer.position]; // 起始帧
  8. animation.toValue = [NSValue valueWithCGPoint:CGPointMake(320, 480)]; // 终了帧
  9. // 添加动画
  10. [myView.layer addAnimation:animation forKey:@"move-layer"];

旋转动画

旋转动画的代码如下:
  1. /* 旋转 */
  2. // 对Y轴进行旋转(指定Z轴的话,就和UIView的动画一样绕中心旋转)
  3. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
  4. // 设定动画选项
  5. animation.duration = 2.5; // 持续时间
  6. animation.repeatCount = 1; // 重复次数
  7. // 设定旋转角度
  8. animation.fromValue = [NSNumber numberWithFloat:0.0]; // 起始角度
  9. animation.toValue = [NSNumber numberWithFloat:22 * M_PI]; // 终止角度
  10. // 添加动画
  11. [myView.layer addAnimation:animation forKey:@"rotate-layer"];

缩放动画

缩放动画的代码如下:
  1. /* 放大缩小 */
  2. // 设定为缩放
  3. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  4. // 动画选项设定
  5. animation.duration = 2.5; // 动画持续时间
  6. animation.repeatCount = 1; // 重复次数
  7. animation.autoreverses = YES; // 动画结束时执行逆动画
  8. // 缩放倍数
  9. animation.fromValue = [NSNumber numberWithFloat:1.0]; // 开始时的倍率
  10. animation.toValue = [NSNumber numberWithFloat:2.0]; // 结束时的倍率
  11. // 添加动画
  12. [myView.layer addAnimation:animation forKey:@"scale-layer"];

组合动画

使用CAAnimationGroup类进行复数动画的组合。代码如下:

  1. /* 动画1(在X轴方向移动) */
  2. CABasicAnimation *animation1 =
  3. [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
  4. // 终点设定
  5. animation1.toValue = [NSNumber numberWithFloat:80];; // 終点
  6. /* 动画2(绕Z轴中心旋转) */
  7. CABasicAnimation *animation2 =
  8. [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  9. // 设定旋转角度
  10. animation2.fromValue = [NSNumber numberWithFloat:0.0]; // 开始时的角度
  11. animation2.toValue = [NSNumber numberWithFloat:44 * M_PI]; // 结束时的角度
  12. /* 动画组 */
  13. CAAnimationGroup *group = [CAAnimationGroup animation];
  14. // 动画选项设定
  15. group.duration = 3.0;
  16. group.repeatCount = 1;
  17. // 添加动画
  18. group.animations = [NSArray arrayWithObjects:animation1, animation2, nil nil];
  19. [myView.layer addAnimation:group forKey:@"move-rotate-layer"];

捕获动画开始时和终了时的事件

博主:设定委托对象,实现委托方法,如下:

  1. /* 移动 */
  2. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
  3. animation.delegate = self; // 指定委托对象
  4. // 设定动画选项
  5. animation.duration = 2.5; // 动画时长
  6. animation.repeatCount = 1; // 重复次数
  7. // 终点设定
  8. animation.toValue = [NSNumber numberWithFloat:100];; // 终点
  9. // 添加动画
  10. [myView.layer addAnimation:animation forKey:@"move-layer"];
  11. ・・・
  12. /**
  13. * 动画开始时
  14. */
  15. - (void)animationDidStart:(CAAnimation *)theAnimation
  16. {
  17. NSLog(@"begin");
  18. }
  19. /**
  20. * 动画结束时
  21. */
  22. - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
  23. {
  24. NSLog(@"end");
  25. }

CABasicAnimation使用时候的注意点

CABasicAnimation正在进行动画的时候,点击了Home按钮后再回到app的时候,动画会被清空。

Objective-C的示例程序

使用CABasicAnimation实现关键帧动画的示例程序如下:

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. // 图像显示
  5. UIImage *image = [UIImage imageNamed:@"image01.png"];
  6. UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
  7. imageView.center = CGPointMake(160, 240);
  8. [self.view addSubview:imageView];
  9. /* 移动 */
  10. CABasicAnimation *animation1 =
  11. [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
  12. // 起止点的设定
  13. animation1.toValue = [NSNumber numberWithFloat:100];; // 終点
  14. /* 旋转 */
  15. CABasicAnimation *animation2 =
  16. [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
  17. // 绕x轴转3圈
  18. animation2.toValue = [NSNumber numberWithFloat:66 * M_PI]; // 结束时的角度
  19. /* 动画组 */
  20. CAAnimationGroup *group = [CAAnimationGroup animation];
  21. group.delegate = self;
  22. group.duration = 5.0;
  23. group.repeatCount = 1;
  24. // 动画结束后不变回初始状态
  25. group.removedOnCompletion = NO;
  26. group.fillMode = kCAFillModeForwards;
  27. // 添加动画
  28. group.animations = [NSArray arrayWithObjects:animation1, animation2, nil nil];
  29. [imageView.layer addAnimation:group forKey:@"move-rotate-layer"];
  30. }
  31. /**
  32. * 动画开始时
  33. */
  34. - (void)animationDidStart:(CAAnimation *)theAnimation
  35. {
  36. NSLog(@"begin");
  37. }
  38. /**
  39. * 动画结束时
  40. */
  41. - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
  42. {
  43. NSLog(@"end");
  44. }

示例程序的执行结果

Objective-C的示例程序的执行结果如下:

控制台输出如下:

    1. begin
    2. end

ios之CABasicAnimation的更多相关文章

  1. iOS开发CABasicAnimation动画理解

    1.CALayer简介 CALayer是个与UIView很类似的概念,同样有backgroundColor.frame等相似的属性,我们可以将UIView看做一种特殊的CALayer.但实际上UIVi ...

  2. iOS Layer CABasicAnimation

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  3. iOS - CABasicAnimation

    代码实例: [1] - (void)pulseClick { //!> 宽和高等比例转换 CABasicAnimation * pulse = [CABasicAnimation animati ...

  4. IOS第18天(5,CABasicAnimation基本动画)

    ******* #import "HMViewController.h" @interface HMViewController () @property (nonatomic, ...

  5. iOS:核心动画之基本动画CABasicAnimation

    基本动画,是CAPropertyAnimation的子类 属性说明: fromValue:keyPath相应属性的初始值 toValue:keyPath相应属性的结束值 动画过程说明: 随着动画的进行 ...

  6. iOS开发——动画编程Swift篇&(四)CABasicAnimation动画

    CABasicAnimation动画 //CABasicAnimation-不透明度 @IBAction func cabOpacity() { let animation = CABasicAnim ...

  7. CABasicAnimation 基本动画 分类: ios技术 2015-07-16 17:10 132人阅读 评论(0) 收藏

    几个可以用来实现热门APP应用PATH中menu效果的几个方法 +(CABasicAnimation *)opacityForever_Animation:(float)time //永久闪烁的动画 ...

  8. iOS核心动画详解(CABasicAnimation)

    前言 上一篇已经介绍了核心动画在UI渲染中的位置和基本概念,但是没有具体介绍CAAnimation子类的用法,本文将介绍CABasicAnimation及其子类CASpringAnimation的用法 ...

  9. AJ学IOS(39)UI之核心动画之CABasicAnimation(基础动画)

    AJ分享,必须精品 一.CABasicAnimation简介 CAPropertyAnimation的子类 属性解析: fromValue:keyPath相应属性的初始值 toValue:keyPat ...

随机推荐

  1. c++ primer 学习杂记1

    读到p483 公有,私有,受保护的继承. 1.关于基类成员在派生类中的访问级别: 1) 无论何种继承方式, 派生类都无法访问基类中的private成员. 2) 派生类可以限制,而不能放松对所继承成员的 ...

  2. 欢迎来到abc2237512422的博客

    这是第一篇博文. 本博客已迁移到 abc233.site

  3. 一步一步学习IdentityServer3 (4)

    其实上述例子 很多都很找到 但是在实际生态环境中给例子有很多不一样的地方 比如自定已登录界面怎么做? 怎么访问自己的用户数据库实现登录? 怎么在接口中使用,在接口中又怎么实现与Idr3结合授权? 等等 ...

  4. 第二届CCF软件能力认证

    1. 相邻数对 问题描述 给定n个不同的整数,问这些数中有多少对整数,它们的值正好相差1. 输入格式 输入的第一行包含一个整数n,表示给定整数的个数. 第二行包含所给定的n个整数. 输出格式 输出一个 ...

  5. Springbatch Miscellanea Notes

    1.scope="step",如下图,这是一种后绑定的方式,生成Step的时候,才去创建bean <bean id="testTasklet" paren ...

  6. 为什么要做A.prototype.constructor=A这样的修正?

    问题 虽然看过这篇博文JavaScript prototype之后对原型理解不再那么模糊了,但是依然还有很多理解不甚透彻的地方.比如,今天看到一个原型式继承的例子,又有些困惑,于是找了些帖子看看,有了 ...

  7. mybatis的快速入门

    说明: 在这个部分,会写个简单的入门案例. 然后,会重新写一个,更加严格的程序案例. 一:案例一 1.最终的目录结构 2.新建一个普通的Java项目,并新建lib 在项目名上右键,不是src. 3.导 ...

  8. Java Socket实战之一 单线程通信基础socket

    现在做Java直接使用Socket的情况是越来越少,因为有很多的选择可选,比如说可以用spring,其中就可以支持很多种远程连接的操作,另外jboss的remoting也是不错的选择,还有Apache ...

  9. Java 持久化之 -- IO 全面整理(看了绝不后悔)

    目录: 一.java io 概述 什么是IO? IO包括输入流和输出流,输入流指的是将数据以字符或者字节形式读取到内存 分为字符输入流和字符输入流 输入流指的是从内存读取到外界 ,分为字符输入流和字节 ...

  10. 隐马尔科夫模型(HMM)与词性标注问题

    一.马尔科夫过程: 在已知目前状态(现在)的条件下,它未来的演变(将来)不依赖于它以往的演变 (过去 ).例如森林中动物头数的变化构成——马尔可夫过程.在现实世界中,有很多过程都是马尔可夫过程,如液体 ...