zoukankan      html  css  js  c++  java
  • CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)

    CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)

    CABasicAnimation类的使用方式就是基本的关键帧动画。

    所谓关键帧动画,就是将Layer的属性作为KeyPath来注册,指定动画的起始帧和结束帧,然后自动计算和实现中间的过渡动画的一种动画方式。

    CABasicAnimation的基本使用顺序

    1.引用QuartzCore.framework

    将"QuartzCore.framework"这个库添加到项目中。并且在需要使用CABaseAnimation类的地方import头文件。

    [objc] view plain copy
     
    1. #import <QuartzCore/QuartzCore.h>  


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

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

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

    3.设定动画

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

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

    例:

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

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

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

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

    例:

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

    5.添加动画

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

    [objc] view plain copy
     
    1. [myView.layer addAnimation:animation forKey:@"move-layer"];  

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

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

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

    CABasicAnimation的使用示例

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

    移动动画

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

    旋转动画

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

    缩放动画

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

    组合动画

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

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

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

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

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

    CABasicAnimation使用时候的注意点

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

    Objective-C的示例程序

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

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

    示例程序的执行结果

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

    控制台输出如下:

    [plain] view plain copy
     
      1. begin  
      2. end 
  • 相关阅读:
    qt截取屏幕
    使用XmlTextReader 读取XML
    QQ2010 SP2 美化 皮肤 修改 透明 托盘 图标 RES.RDB 解包 打包 去广告 显IP
    发一个linux串口监视工具
    linux打包压缩命令汇总
    Qt实现遍历文件夹和文件目录(递归)
    linux忘记root密码的恢复方法
    centos x8664位版本 想安装qq for linux
    删除所有的.svn文件夹
    qtsdk1.2.1 静态编译
  • 原文地址:https://www.cnblogs.com/YangFuShun/p/7992778.html
Copyright © 2011-2022 走看看