iOS-Senior17-CoreAnimation动画
Posted 萌萌的周丽娜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS-Senior17-CoreAnimation动画相关的知识,希望对你有一定的参考价值。
1.CABasicAnimation动画
//设置全局变量图片
@property (nonatomic,strong) UIImageView *eyeImage;
//设置圆角(注意:光设置上边一句代码是实现不了效果的,下边的masksToBounds这个属性影响layer层的阴影效果)
self.eyeImage.layer.cornerRadius = 40;
self.eyeImage.layer.masksToBounds = YES;
//设置阴影
//设置layer的阴影颜色
self.eyeImage.layer.shadowColor = [UIColor yellow].CGColor;
//设置layer层的透明度
self.eyeImage.layer.shadowOpacity = 1.0;
//设置阴影的偏移量
self.eyeImage.layer.shadowOffSet = CGSizeMake(-20,10);
//设置阴影的模糊度
self.eyeImage.layer.shadowRadius = 1.0f;
##########自定义layer######################
//创建一个对象
CALayer *layer = [CALayer layer];
//设置对象的位置和大小
layer.frame = CGRectMake(0,280,100,100);
//设置背景颜色
layer.backgroundColor = [UIColor redColor].CGColor;
//layer需要添加到layer层
[self.view.layer addSublayer:layer];
//设置锚点
layer.anchorPoint = CGPointMake(0,0);
//设置大小(位置)
layer.position = CGPointMake(100,100);
CABasicAnimation动画
//第一步:创建动画的对象
CABasicAnimation *basicAnimation = [CABasicAnimation animation];
//第二步:告诉layer层需要执行什么样子的动画[后边设置的内容为CALayer的相关属性]
basicAnimation.keyPath = @"position";
//第三步:告诉从哪里来,到哪里去
basicAnimation.fromValue = [NSValue valueWithCGPoint : CGPointMake(100,100)];
basicAnimation.toValue = [NSValue valueWirhCGPoint : CGPointMake(300,300)];
//注意点:如果要实现移动的位置不回到原来的位置,需要实现以下两句代码
basicAnimation.removeOnCompletion = NO;
//设置保存动画状态的内容
basicAnimation.fillMode = KCAFillModeForwards;
//第四步:设置动画持续时长
basicAnimation.duration = 6.0f;
//第五步:将要执行的动画添加到calayer层
[self.eyeImage.layer addAnimation:basicAnimation forKey:@"basic"];
###############翻转效果#######################
CABasicAnimation *basic = [CABasicAnimation animation];
basic.keyPath = @"transform";
//设置翻转到的地方
basic.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2,0,0,1)];
basic.duration = 0.2f;
[self.eyeImage.layer addAnimation:basic forKey:@"aaa"];
//根据key区移除动画
[self.eyeImage.layer removeAnimationForKey:@"basic"];
2.CAKeyFrameAnimation动画
//第一步:创建对象
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animation];
//第二步:设置动画轨迹
keyFrameAnimation.keyPath = @"transform.rotation";
//第三步:设置旋转角度(弧度计算公式:度数/180*M_PI)
keyFrameAnimation.values = @[@(-4/180.0 *M_PI),@(4/180.0 *M_PI),@(8/180 *M_PI)];
//第四步:设置时长
keyFrameAnimation.Duration = 3.0f;
//第五步:添加动画到layer层
[self.eyeImage.layer addAnimation:keyFrameAnimation forKey:@"keyFrameAnimation"];
3.CAAnimationGroup动画
//平移动画
CABasicAnimation *basicAnimation1 = [CABsicAnimation animation];
basicAnimation1.keyPath = @"transform.translation.y";
basicAnimation1.toValue = @(400);
//翻转动画
CABasicAnimation *basicAnimation2 = [CABasicAnimation animation];
basicAnimation2.keyPath = @"transform.scale";
basicAnimation2.toValue = @(0.2);
//旋转动画
CABasicAnimation *baiscAnimation3 = [CABasicAnimation animation];
basicAnimation3.keyPath = @"transform.rotation";
basicAnimation3.toValue = @(M_PI);
//需要创建管理各个动画的动画组
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[basicAnimation1,basicAnimation2,basicAnimation3];
group.duration = 5.0f;
[self.eyeImage.layer addAnimation:group forKey:@"groupAnimation"];
4.CASpringAnimation动画
CASpringAnimation *springAnimation = [CASpringAnimation animation];
springAnimation.keyPath = @"transform.scale";
springAnimation.fromValue = @1;
springAnimation.toValue = @0.25;
springAnimation.duration = 2.0f;
[self.eyeImage.layer addAnimation :springAnimation forKey:@"springAnimation"];
以上是关于iOS-Senior17-CoreAnimation动画的主要内容,如果未能解决你的问题,请参考以下文章