CAAnimation(抽象类)
— CATransiton(用于做转场动画,iOS中比os少。UINavicationController的pop、push效果)
— CAPropertyAnimation(抽象类)
— CABasicAnimation(具体类,只能从一个数值变化到另一个数值)
— CAKeyframeAnimation(具体类,使用一个数值保存变化范围)
2.动画分类
通过动画上下文使用UIKit动画
//开始动画 [UIView beginAnimations:@"test" context:nil]; //动画时长 [UIView setAnimationDuration:1]; redView.frame=CGRectMake(50, 50, 200, 200); //动画结束 [UIView commitAnimations];
通过代码块使用UIKit动画
[UIView animateWithDuration:1 //时长 delay:0 //延迟时间 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{ redView.backgroundColor=[UIColor blueColor]; } completion:^(BOOL finish){ }];
使用Core Animation对象来实现动画
其中CABasicAnimation和CAKeyframeAnimation是对图层中的不同属性进行动画的。
如果要多整个图层进行动画,则应该使用CATransition,CATransition是CAAnimation的子类
如果要使用组合动画,例如要改变图层的大小和透明度,则可以先为每个属性创建一个CABasicAnimation对象,再把他们组合到CAAnimationGroup中,最后把这个组合添加到要进行动画的CALayer中
自定义动画:CABasicAnimation
//创建一个CABasicAnimation对象 CABasicAnimation *animation=[CABasicAnimation animation]; //设置颜色 animation.toValue=(id)[UIColor blueColor].CGColor; //动画时间 animation.duration=1; //是否反转变为原来的属性值 animation.autoreverses=YES; //把animation添加到图层的layer中,便可以播放动画了。forKey指定要应用此动画的属性 [self.view.layer addAnimation:animation forKey:@"backgroundColor"];
关键帧动画:CAKeyframeAnimation
1. path
这是一个 CGPathRef 对象,默认是空的,当我们创建好CAKeyframeAnimation的实例的时候,可以通过制定一个自己定义的path来让 某一个物体按照这个路径进行动画。这个值默认是nil 当其被设定的时候 values 这个属性就被覆盖
2. values
一个数组,提供了一组关键帧的值, 当使用path的 时候 values的值自动被忽略。
下面是改变依次改变view的颜色
3、通过设置属性keyTimes来设定每一帧的时间
4、示例代码
//初始化一个View,用来显示动画 CAKeyframeAnimation *ani=[CAKeyframeAnimation animation]; //初始化路径 CGMutablePathRef aPath=CGPathCreateMutable(); //动画起始点 CGPathMoveToPoint(aPath, nil, 20, 20); CGPathAddCurveToPoint(aPath, nil, 160, 30, 220, 220, 240, 380); ani.path=aPath; ani.duration=10; //设置为渐出 ani.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; //自动旋转方向 ani.rotationMode=@"auto"; [redView.layer addAnimation:ani forKey:@"position”];
组合动画CAAnimationGroup
CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position”]; .****** CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"]; ******* CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"alpha"]; ******* //关键帧,旋转,透明度组合起来执行 CAAnimationGroup *animGroup = [CAAnimationGroup animation]; animGroup.animations = [NSArray arrayWithObjects:moveAnim, scaleAnim,opacityAnim, nil]; animGroup.duration = 4; [self.imageView.layer addAnimation:animGroup forKey:nil];