CABasicAnimation初始化通过animationwithkeypath的Key值设置动画

本文介绍如何使用CABasicAnimation和CAAnimationGroup等类创建复杂的UI动画效果,包括位置移动、透明度变化、旋转和平移等多种动画类型,并展示了如何通过组合不同类型的动画来达到更丰富的视觉效果。

转自:http://www.jianshu.com/p/d42012d3588c


transform.scale = 比例轉換

transform.scale.x = 闊的比例轉換

transform.scale.y = 高的比例轉換

transform.rotation.z = 平面圖的旋轉

opacity = 透明度

margin = 布局

zPosition = 翻转

backgroundColor = 背景颜色

cornerRadius = 圆角

borderWidth = 边框宽

bounds = 大小

contents = 内容

contentsRect = 内容大小

cornerRadius = 圆角

frame = 大小位置

hidden = 显示隐藏

mask

masksToBounds

opacity

position

shadowColor

shadowOffset

shadowOpacity

shadowRadius

实例几种动画初始化:

  1. //界限  
  2.     CABasicAnimation *boundsAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];  
  3.     boundsAnimation.fromValue = [NSValue valueWithCGRectself.ViewTest.bounds];  
  4.     boundsAnimation.toValue = [NSValue valueWithCGRect:CGRectZero];  
  5.     //透明度变化  
  6.     CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];  
  7.     opacityAnimation.fromValue = [NSNumber numberWithFloat:1.0];  
  8.     opacityAnimation.toValue = [NSNumber numberWithFloat:0.5];  
  9.       
  10.     //位置移动  
  11.     CABasicAnimation *animation  = [CABasicAnimation animationWithKeyPath:@"position"];  
  12.     animation.fromValue =  [NSValue valueWithCGPointself.ViewTest.layer.position];  
  13.     CGPoint toPoint = self.ViewTest.layer.position;  
  14.     toPoint.x += 180;  
  15.     animation.toValue = [NSValue valueWithCGPoint:toPoint];  
  16.       
  17.     //旋转动画  
  18.     CABasicAnimation* rotationAnimation =  
  19.     [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];//"z"还可以是“x”“y”,表示沿z轴旋转  
  20.       
  21.     rotationAnimation.toValue = [NSNumber numberWithFloat:(22 * M_PI) * 3];  
  22.     // 3 is the number of 360 degree rotations  
  23.       
  24.     // Make the rotation animation duration slightly less than the other animations to give it the feel  
  25.     // that it pauses at its largest scale value  
  26.     rotationAnimation.duration = 3.0f;  
  27.     rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; //缓入缓出  
  28.     //rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];  
  29.       
  30.     //缩放动画  
  31.     CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];  
  32.     scaleAnimation.fromValue = [NSNumber numberWithFloat:0.0];  
  33.     scaleAnimation.toValue = [NSNumber numberWithFloat:1.0];  
  34.     scaleAnimation.duration = 3.0f;  
  35.     scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];  
  36.     //组合动画  
  37.     CAAnimationGroup *animationGroup = [CAAnimationGroup animation];  
  38.     animationGroup.duration = 3.0f;  
  39.     animationGroup.autoreverses = YES;   //是否重播,原动画的倒播  
  40.     animationGroup.repeatCount = NSNotFound;//HUGE_VALF;     //HUGE_VALF,源自math.h  
  41.     [animationGroup setAnimations:[NSArray arrayWithObjects:rotationAnimation, scaleAnimation,boundsAnimation, nil nil]];  
  42.       
  43.     //将上述动画编组应用  
  44.       
  45.     [self.ViewTest.layer addAnimation:animationGroup forKey:@"animationGroup"];  


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值