在iOS中核心动画分为几类:基础动画(CABasicAnimation)、关键帧动画(CAKeyframeAnimation)、动画组(CAAnimationGroup)、转场动画(CATransition)
CAAnimation:核心动画的基础类,不能直接使用,负责动画运行时间、速度的控制,本身实现了CAMediaTiming协议。
CAPropertyAnimation:属性动画也是基类(通过属性进行动画设置,注意是动画属性),不能直接使用,要使用他的子类。
CAPropertyAnimation的子类
{CABasicAnimation:基础动画,通过属性修改进行动画参数控制,只有初始状态和结束状态。
CAKeyframeAnimation:关键帧动画,同样是通过属性进行动画参数控制,但是同基础动画不同的是它可以有多个状态控制。}
CAAnimationGroup:动画组,动画组是一种组合模式设计,可以通过动画组来进行所有动画行为的统一控制,组中所有动画效果可以并发执行。
CATransition:转场动画,主要通过滤镜进行动画效果设置两个不同场景的转换。
基础动画、关键帧动画都属于属性动画,就是通过修改属性值产生动画效果,开发人员只需要设置初始值和结束值,中间的过程动画(又叫“补间动画”)由系统自动计算产生。和基础动画不同的是关键帧动画可以设置多个属性值,每两个属性中间的补间动画由系统自动完成,因此从这个角度而言基础动画又可以看成是有两个关键帧的关键帧动画
创建基础动画 需要通过fromValue 和 toValue 属性来指定一个开始值和初始值 当添加基础动画到图层中的时候 它才会开始变化
autoreverses:当设置这个属性为YES时,在它达到目的地之后,会以动画的方式返回到开始的值
duration 设置持续时间 期间会被属性影响
speed 默认的值为 1.0.这意味着动画播放按照默认的速度。如果你改变这个值为 2.0,动画会用 2 倍的速度播放。 这样的影响就是使持续时间减半。如果你指定的持续时间为 6 秒,速度为 2.0,动画就会播放 3 秒钟---一半的 持续时间
把速度设置成0 就可以暂停动画
repeatCount 默认的值是0,意味着动画只会播放一次 这个不应该和repeatDuration 属性一块使用。(负数不是无限循环)
repeatDuration 这个属性指定了动画应该被重复多久。动画会一直重复,直到设定的时间流逝完。它不应该和 repeatCount 一起使用
timingFunction 速度控制函数,控制动画运行的节奏
timingFunction 属性值:
kCAMediaTimingFunctionLinear(线性):匀速,给你一个相对静态的感觉
kCAMediaTimingFunctionEaseIn(渐进):动画缓慢进入,然后加速离开
kCAMediaTimingFunctionEaseOut(渐出):动画全速进入,然后减速的到达目的地
kCAMediaTimingFunctionEaseInEaseOut(渐进渐出):动画缓慢的进入,中间加速,然后减速的到达目的地。这个是默认的动画行为。
removedOnCompletion 默认为YES,代表动画执行完毕后就从图层上移除,图形会恢复到动画执行前的状态。如果想让图层保持显示动画执行后的状态,那就设置为NO,不过还要设置fillMode为kCAFillModeForwards
fillMode 设置当前对象在非活动时间段的行为 比如动画开始之前或者动画结束之后
fillMode属性值(上面提到过 要想fillMode有效,需要设置removedOnCompletion = NO)
kCAFillModeRemoved 这个是默认值,也就是说当动画开始前和动画结束后,动画对layer都没有影响,动画结束后,layer会恢复到之前的状态
kCAFillModeForwards 当动画结束后,layer会一直保持着动画最后的状态
kCAFillModeBackwards 在动画开始前,只需要将动画加入了一个layer,layer便立即进入动画的初始状态并等待动画开始。
kCAFillModeBoth 这个其实就是上面两个的合成.动画加入后开始之前,layer便处于动画初始状态,动画结束后layer保持动画最后的状态
CAPropertyAnimation
可以通过改变animationWithKeyPath来改变动画的属性:
transform.scale = 比例转换
#import "ViewController.h"
@interface ViewController ()
{
CALayer *showLayer;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIImage *image = [UIImage imageNamed:@"心"];
showLayer = [[CALayer alloc]init];
showLayer.bounds = CGRectMake(0, 0, image.size.width/2, image.size.height/2);
showLayer.position = self.view.center;
showLayer.contents = (id)image.CGImage;
[self.view.layer addSublayer:showLayer];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(animation1)];
[self.view addGestureRecognizer:tap];
}
#pragma mark-------------------改变Position-------
- (void)animation1{
// CABasicAnimation属于属性动画 需要告诉它 咱们要改变属性 是哪个 (把属性当做字符串传递)
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
// toValue 设置动画 要到哪个位置
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(400, showLayer.position.y)];
animation.duration = 3;
// 以动画效果 回到 原来的位置
animation.autoreverses = YES;
// 如果 使用 fillMode 必须禁用removedOnCompletion
// animation.removedOnCompletion = NO;
// animation.fillMode = kCAFillModeBoth;
// 设置 慢进慢出
// CAMediaTimingFunction
/*
CA_EXTERN NSString * const kCAMediaTimingFunctionLinear
__OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseIn
__OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseOut
__OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseInEaseOut
__OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
CA_EXTERN NSString * const kCAMediaTimingFunctionDefault
__OSX_AVAILABLE_STARTING (__MAC_10_6, __IPHONE_3_0);
*/
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
// 添加动画到图层 forKey 动画名字
[showLayer addAnimation:animation forKey:@"move1"];
}
#pragma mark--------改变bounds---------
- (void)animation2{
UIImage *image = [UIImage imageNamed:@"8fac777"];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];
animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, image.size.width/5, image.size.height/5)];
animation.duration = 0.5;
animation.autoreverses = YES;
// repeatCount 属性时 -1 不是无限循环 HUGE 无极大
animation.repeatCount = HUGE;
[showLayer addAnimation:animation forKey:@"jump"];
}
#pragma mark--------抖动---------
- (void)animation3{
// 基础动画 继承自 属性动画 通过属性名当做一个key来确定围绕那个属性 进行动画
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = @(-0.1);
animation.toValue = @(0.1);
animation.duration = 0.1;
animation.repeatCount = HUGE;
animation.autoreverses = YES;//是否允许以动画的效果回去
[showLayer addAnimation:animation forKey:@"douDong"];
}
- (void)animation4{
showLayer.anchorPoint = CGPointMake(1, 1);
// 基础动画 继承自 属性动画 通过属性名当做一个key 来确定围绕哪个属性 进行动画
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
// animation.fromValue = @(-0.1);
animation.toValue = @(1);
animation.duration = 5;
animation.repeatCount = 2;
// 是否以动画的方式返回
animation.autoreverses = YES;
[showLayer addAnimation:animation forKey:@"shake"];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self animation3];
}