核心动画 Core Animation
###核心动画(Core Animation)是一组非常强大的动画处理API,使用它能做出非常绚丽的动画效果,而且往往事半功倍 ####CAAnimation是所有动画对象的基类,负责控制动画的持续时间和速度,是个抽象类,不能直接使用,应该使用它的具体子类 1.动画是否自动移除-removeOnCompletion
2.动画持续时间-duration
3.速度-speed
4.动画时间的偏移-timeOffset
5.动画重复执行的次数-repeatCount(最大次数HUGE_VALF)
6.动画重复执行的总时间-repeatDuration
7.反转动画-autoreverses
8.代理-delegate
9.填充模式-fillMode
10.速度控制函数-timingFunction
示例-基本动画
1.平移-position
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
// 起始点
animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(150, 50)];
// 结束点
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(150, 550)];
// 动画持续时间
animation.duration = 2.0;
// 填充模式
animation.fillMode = kCAFillModeForwards;
// 动画是否自动移除
animation.removedOnCompletion = NO;
// 反转动画
animation.autoreverses = YES;
// 动画重复执行次数
// HUGE_VALF 最大浮点数,表示无限次重复
animation.repeatCount = HUGE_VALF;
// 速度控制函数
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
/* 动画的线性变换(动画速度变化)
kCAMediaTimingFunctionLinear 匀速
kCAMediaTimingFunctionEaseIn 加速
kCAMediaTimingFunctionEaseOut 减速
kCAMediaTimingFunctionEaseInEaseOut 缓慢进入缓慢出去
kCAMediaTimingFunctionDefault 默认
*/
// 播放动画的速度
animation.speed = 2;
// 添加动画 @""里面的是标识
[self.view.layer addAnimation:[self positionAnimation] forKey:@"position"];
2.旋转-transform.rotation
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
// animation.fromValue = @0;
// 可以为负数 反旋转
// animation.toValue = @(2 * M_PI);
animation.byValue = @( 2 * M_PI);
animation.duration = 2.0;
animation.repeatCount = HUGE_VALF;
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
animation.autoreverses = YES;
[self.view.layer addAnimation:[self positionAnimation] forKey:@"rotation"];
3.跳动-bounds
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];
animation.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 100, 100)];
animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 300, 300)];
animation.duration = 2.0;
[self.view.layer addAnimation:[self positionAnimation] forKey:@"bounds"];
4.缩放-scale
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
// 1、初始值
animation.fromValue = @1.0;
// 2、目标值
animation.toValue = @2.0;
// 3、变化的值, fromValue ~ toValue 值的变化量
animation.duration = 2.0;
/* 4、动画的填充模式:
kCAFillModeForwards
kCAFillModeBackwards
kCAFillModeBoth
kCAFillModeRemoved
*/
animation.fillMode = kCAFillModeForwards;
[self.view.layer addAnimation:[self positionAnimation] forKey:@"scale"];