UIView是iOS系统中界面元素的基础,所有的界面元素都是继承自它。它本身完全是由CoreAnimation来实现的。它真正的绘图部分,是由一个CALayer类来管理。所以在Layer层来做复杂的大动画对于资源的占用会更少一些。
一些Layer属性设置:
//CALayer 负责渲染绘制视图
//UIView 负责交互----UIView自带有一个layer属性
//圆角设置
self.view4calayerAnimation.layer.cornerRadius = 100;
//设置阴影颜色(注意要使用CGColor)
self.view4calayerAnimation.layer.shadowColor = [[UIColor blueColor]CGColor];
//设置阴影偏移
self.view4calayerAnimation.layer.shadowOffset = CGSizeMake(20, 20);
//设置模糊度
self.view4calayerAnimation.layer.shadowRadius = 10;
//设置透明度
self.view4calayerAnimation.layer.shadowOpacity = 0.5;
//设置边框
self.view4calayerAnimation.layer.borderColor = [[UIColor yellowColor]CGColor];
self.view4calayerAnimation.layer.borderWidth = 5;
CABasicAnimation:
//position是当前空间的锚点位于父视图中的(x,y)坐标
// 无法使用frame去做动画
//1.创建一个基础动画
CABasicAnimation *baseAnimition1 = [CABasicAnimation animationWithKeyPath:@"bounds"];
//起始值
//toValue 是一个id类型的对象,而rect是一个结构体,我们需要把结构体转成对象才可以
baseAnimition1.fromValue =[NSValue valueWithCGRect:self.view4calayerAnimation.bounds];
baseAnimition1.toValue = [NSValue valueWithCGRect:CGRectMake(100, 100, 100, 100)];
baseAnimition1.duration = 1;
//添加到layer的动画中去
[self.view4calayerAnimation.layer addAnimation:baseAnimition1 forKey:@"动画1"];
CAKeyframeAnimation:
//创建关键路径动画
CAKeyframeAnimation *keyform = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//设置动画时长
keyform.duration = 5;
//设置路径
keyform.values = @[[NSValue valueWithCGPoint:CGPointMake(320, 0)],[NSValue valueWithCGPoint:CGPointMake(320, 100)],[NSValue valueWithCGPoint:CGPointMake(420, 100)],[NSValue valueWithCGPoint:CGPointMake(420, 0)]];
//重复次数
keyform.repeatCount = 1;
//提交动画
[self.view4calayerAnimation.layer addAnimation:keyform forKey:@"路径"];
CAAnimationGroup:
//创建一个群组,用于放我们的动画
CAAnimationGroup *group = [CAAnimationGroup animation];
//创建关键路径动画
CAKeyframeAnimation *keyform = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//设置动画时长
keyform.duration = 3;
//设置路径
keyform.values = @[[NSValue valueWithCGPoint:CGPointMake(100, 100)],[NSValue valueWithCGPoint:CGPointMake(100, 500)],[NSValue valueWithCGPoint:CGPointMake(300, 500)],[NSValue valueWithCGPoint:CGPointMake(300, 100)],[NSValue valueWithCGPoint:CGPointMake(100, 100)]];
//重复次数
keyform.repeatCount = 1;
//提交动画
[self.view4calayerAnimation.layer addAnimation:keyform forKey:@"路径"];
CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"bounds"];
CGRect rect = self.groupView1.bounds;
basic.fromValue = [NSValue valueWithCGRect:self.groupView1.bounds];
//rect.size.width += 200;
//rect.size.height += 200;
basic.toValue = [NSValue valueWithCGRect:rect];
basic.duration = 5;
basic.repeatCount = 1;
basic.autoreverses = YES;
//将创建的动画添加到动画数组中去
group.animations = @[keyform, basic];
//给group添加执行时间
//只有把动画属性都添加到group中才能生效
group.duration = 5;
//重复次数
group.repeatCount = 1;
//将动画群组加到我们的空间layer上,动画开始执行
[self.view4calayerAnimation.layer addAnimation:group forKey:@"group"];
//同一个动画数组可以添加到多个视图layer上
//[self.groupView1.layer addAnimation:group forKey:@"test1"];