iOS中核心动画分为:基本动画、关键帧动画、转场动画、动画组.(layer中不能真实改变图层动画值,我们所看到的动画只是假象,一旦程序切换到后台运行后动画就会停止,所以一般只用于转场动画)。
@property (nonatomic, strong) UIView *myview; //定义一个控件
-(void)viewDidLoad{
[super viewDidLoad];
self.myview = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 60, 60)];
_myview.backgroundColor =[UIColor redColor];
_myview.layer.cornerRadius = _myview.frame.size.width/2;
[self.view addSubview:_myview]; //添加要做动画的控件
// [self baseAnimation];
// [self keyframeAnimation];
// [self rolePathAnimation];
[self animationGroup];
}
#define UIColorRandom [UIColor colorWithRed:arc4random()%255 green:arc4random()%255 blue:arc4random()%255 alpha:1.0]
1.基本动画
-(void)baseAnimation{
//创建动画对象
CABasicAnimation *anim = [CABasicAnimation animation];
//设置动画
// anim.keyPath = @"position"; //调整哪个属性,执行怎样的动画,查看view.layer里面有什么属性才能填写什么属性,比如设置动画@"bounds":缩放,@"position":平移
// anim.toValue =[NSValue valueWithCGPoint:CGPointMake(200, 100)];
anim.keyPath = @"transform";// 设置transform格式的path @"transform.rotation"则anim.toValue =@(M_PI_2) @"transform.rotation.x"则anim.toValue =@(1.5)
// anim.fromValue从该状态开始动画(一般不用做设置),最终变为toValue,增加多少byValue
anim.toValue =[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1, 1, 0)]; //沿着(1,1,0)这条线旋转角度M_PI
anim.duration = 2.0;
anim.removedOnCompletion = NO; //动画完成后是否移除,默认为YES
anim.fillMode = kCAFillModeForwards; //动画结束后也一直保持着动画的最终状态
//添加动画
[self.myview.layer addAnimation:anim forKey:nil];
}
2.关键帧动画
-(void)keyframeAnimation{
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"position"; //位置移动
NSValue *v1 = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake(250, 100)];
NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake(250, 300)];
NSValue *v4 = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
anim.values = @[v1,v2,v3,v4]; //设置一些关键帧,让控件按照这些点去移动
// anim.keyTimes = @[@(0.5),@(0.5),@(0.5)]; //每两个帧之间动画持续时间
anim.duration = 2.0;
//动画执行完毕就停在结束位置
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
//添加动画
[self.myview.layer addAnimation:anim forKey:nil];
}
//除去设置一些关键点做关键帧动画,还可以为动画设置路径
-(void)rolePathAnimation{
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"position";
CGMutablePathRef path = CGPathCreateMutable();//指定路径
CGPathAddEllipseInRect(path, NULL, CGRectMake(100, 100, 200, 300));
anim.path = path;//添加路径,按路径去走
CGPathRelease(path);
//设置动画执行节奏
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; //渐进式(缓慢进入快速移出)
anim.duration = 2.0;
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;//保持最新的状态
anim.delegate = self; //为动画设置代理,可以监听动画的开始/结束
anim.beginTime = CACurrentMediaTime()+3; //在当
//添加动画
[self.myview.layer addAnimation:anim forKey:nil];
}
//动画的代理方法:
-(void)animationDidStart:(CAAnimation *)anim{
NSLog(@"监听动画开始");
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
NSLog(@"监听动画结束");
}
3.转场动画(不同页面之间的切换形式)
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{
_index++;
if (self.index ==10) {
_index = 0;
}
[self transitionAnimation]; //点击切换
}
-(void)transitionAnimation{
_myview.layer.backgroundColor = UIColorRandom.CGColor;
CATransition *anim = [CATransition animation];
anim.type = @"cube"; //转换形式
anim.subtype = kCATransitionFromLeft; //从哪个方向进来
anim.duration = 0.5;
anim.startProgress = 0.5; //从哪个位置开始
anim.endProgress = 0.1;//翻到什么程度消失
[self.myview.layer addAnimation:anim forKey:nil];
}
4.动画组
-(void)animationGroup{
//添加组动画:1.定义动画组 2.定义一些基本动画或者帧动画(注意动画的路径和终点值)添加到动画组 3.设置动画组的属性值 4.在view.layer上添加该动画组
CAAnimationGroup *group = [CAAnimationGroup animation];
//旋转
CABasicAnimation *rotate = [CABasicAnimation animation];
rotate.keyPath = @"transform.rotation";
rotate.toValue = @(M_PI);
//缩放
CABasicAnimation *scale = [CABasicAnimation animation];
scale.keyPath = @"transform.scale";
scale.toValue = @(0.0);
//平移
CABasicAnimation *move = [CABasicAnimation animation];
move.keyPath = @"transform.translation";
move.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)];
group.animations = @[rotate,scale,move];
group.duration = 2.0; //动画持续时间
group.removedOnCompletion = NO; //动画完成后是否移除,默认为YES
group.fillMode = kCAFillModeForwards; //动画结束后也一直保持着动画的最终状态
group.repeatCount = 10; //动画执行次数,(MAXFLOAT无数次)
group.beginTime = CACurrentMediaTime()+3; //图层当前时间再延迟3秒
[self.myview.layer addAnimation:group forKey:nil];
}
添加动画[self.myview.layer addAnimation:anim forKey:@"mykey"];//key是一个表示本动画的标识
[self.myview.layer removeAnimationForKey:@"mykey"];//移除动画
一般如果想要动画结束后位置、大小等值真正改变,还是要在view中做变动,而不是layer中
view和layer的区别:每一个view中都铺有一层layer,layer中有一个GraphiceContext;上下文中可以画出各种图形,也就是layer展示出来的各种控件;view可以处理事件,layer不可以。