IOS代码布局(八) 动画CABasicAnimation

原文链接1:http://blog.csdn.net/iosevanhuang/article/details/14488239

原文链接2:http://www.jianshu.com/p/02c341c748f9

(一)实例化

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

CABasicAnimation *transformAnima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];

  其中,常用的animationWithKeyPath值的总结

说明使用形式
transform.scale 比例转化 @(0.8)
transform.scale.x 宽的比例 @(0.8)
transform.scale.y 高的比例 @(0.8)
transform.rotation.x 围绕x轴旋转 @(M_PI)
transform.rotation.y 围绕y轴旋转 @(M_PI)
transform.rotation.z 围绕z轴旋转 @(M_PI)
cornerRadius 圆角的设置 @(50)
backgroundColor 背景颜色的变化 (id)[UIColor purpleColor].CGColor
bounds 大小,中心不变 [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
position 位置(中心点的改变) [NSValue valueWithCGPoint:CGPointMake(300, 300)];
contents 内容,比如UIImageView的图片 imageAnima.toValue = (id)[UIImage imageNamed:@"to"].CGImage;
opacity 透明度 @(0.7)
contentsRect.size.width 横向拉伸缩放 @(0.4)最好是0~1之间的

2、属性

设定动画的属性和说明

属性说明
duration 动画的时长
repeatCount 重复的次数。不停重复设置为 HUGE_VALF
repeatDuration 设置动画的时间。在该时间内动画一直执行,不计次数。
beginTime 指定动画开始的时间。从开始延迟几秒的话,设置为【CACurrentMediaTime() + 秒数】 的方式
timingFunction 设置动画的速度变化
autoreverses 动画结束时是否执行逆动画
fromValue 所改变属性的起始帧
toValue 终了帧(绝对值)
byValue 终了帧(相对值)

3、防止动画结束后回到初始状态

    animation.removedOnCompletion =NO;
    animation.fillMode = kCAFillModeForwards;

4、添加动画

[self.imageView.layer addAnimation:transformAnima forKey:@"A"];

 

(二)几种动画示例

1、移动:

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"]; 

 

2、旋转

// 对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"];  

 

3、缩放

// 设定为缩放  
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"]; 

 

4、组合动画

/* 动画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"]; 

 

posted @ 2016-10-31 14:21  贾辰  阅读(316)  评论(0编辑  收藏  举报