//每一个UIView,都存在一个CALayer.(主层)
//CALayer的功能 描边,圆角,阴影。。。
//CALayer 属于QuartzCore绘图框架
//明明有UIColor,为啥用CGColorRef?CGColorRef属于QuartzCore,可以使用在mac上。
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.image = [UIImage imageNamed:@"green"];
imageView.layer.borderColor = [UIColor redColor].CGColor;
imageView.layer.borderWidth = 2;
NSLog(@"contents - %@",imageView.layer.contents);
//设置圆角
imageView.layer.cornerRadius = 50;
//隐藏圆形以外的部分
imageView.layer.masksToBounds = YES;
//角度换弧度
#define angleToRadian(angle) angle * M_PI / 180
//z = 1 绕着z轴旋转45
imageView.layer.transform = CATransform3DMakeRotation(angleToRadian(45), 0, 0, 1);
//给CALayer添加图片
CALayer * layer = [CALayer layer];
layer.frame = CGRectMake(100, 100, 100, 100);
layer.backgroundColor = [UIColor greenColor].CGColor;
layer.contents = (id)[UIImage imageNamed:@"111"].CGImage;
[self.view.layer addSublayer:layer];
NSLog(@"contents - %@",layer.contents);
//position位置,是以父视图为参考系,默认是center
//anchorPoint锚点:锚点的范围0~1,默认(0.5,0.5)
imageView.layer.anchorPoint = CGPointMake(0 ,0);
imageView.layer.position = CGPointMake(100 , 100);
2、CALayer隐式动画
//什么时候使用CALayer,什么时候使用UIView?
//当视图需要响应时使用UIView,仅作为展示使用CALayer.
//隐式动画只存在于CALayer的子层上
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
_layer.transform = CATransform3DMakeTranslation(arc4random_uniform(100), arc4random_uniform(100), arc4random_uniform(100));
}
3、CABasicAnimation 基础动画
核心动画:QuartzCore / CoreAnimation / CAAnimation
CAAnimation 是一个抽象类,mac,iOS 通用。
动画添加在CALayer上的。
CABasicAnimation 基础动画 -> CAPropertyAnimation 属性动画 -> CAAnimation
CAKeyframeAnimation 关键帧动画->CAPropertyAnimation 属性动画 -> CAAnimation
CAAnimationGroup 动画组
//创建基础动画
CABasicAnimation * basic = [CABasicAnimation animation];
//动画路径
basic.keyPath = @"position";
//设置初始状态
basic.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
//终止状态
basic.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)];
//动画距离
basic.byValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
//动画的初始和结束的模式 fillMode和removedOnCompletion要配合使用
basic.fillMode = kCAFillModeForwards;// @"forwards";
//动画完成在layer中删除
basic.removedOnCompletion = NO;
//如果初始状态就是当前动画未执行的位置
//动画时间
basic.duration = 1;
//动画的加速度
basic.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
//animation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.5 :0 :0.9 :0.7];
//添加动画
[_animationView.layer addAnimation:basic forKey:@"basic”];
4、CAKeyframeAnimation关键帧动画
//创建关键帧动画
CAKeyframeAnimation * keyframe = [CAKeyframeAnimation animation];
keyframe.keyPath = @"position.x";
//设置关键帧的位置
keyframe.values = @[@0,@10,@-10,@10,@0];
//设置关键帧的时间
keyframe.keyTimes = @[ @0, @(1 / 6.0), @(3 / 6.0), @(5 / 6.0), @1 ];
//无需设置起始位置
keyframe.additive = YES;
//calculationMode设置加速度,kCAAnimationPaced保持恒定的加速度,如果设置calculationMode,keyTimes失效
keyframe.calculationMode = kCAAnimationPaced;
//设置时间
keyframe.duration = 0.5;
//重复次数
keyframe.repeatCount = HUGE_VALF; //MAXFLOAT;
[_animationView.layer addAnimation:keyframe forKey:@"keyframe”];
5、CATransition 转场动画
CATransition * transition = [CATransition animation];
transition.type = @"suckEffect";
transition.subtype = @"fromLeft";
transition.duration = 2;
[self.imageView.layer addAnimation:transition forKey:nil];
6、CAAnimationGroup 动画组
CABasicAnimation * basic1 = [CABasicAnimation animation];
basic1.keyPath = @"transform.scale";
basic1.toValue = @(arc4random_uniform(5));
basic1.duration = 0.5;
basic1.removedOnCompletion = NO;
basic1.fillMode = @"forwards";
[self.animationView.layer addAnimation:basic1 forKey:@"aaa"];
CABasicAnimation * basic2 = [CABasicAnimation animation];
basic2.keyPath = @“tran";
basic2.toValue = @(arc4random_uniform(M_PI));
basic2.duration = 0.5;
basic2.beginTime = 0.5;
[self.animationView.layer addAnimation:basic2 forKey:nil];
CAAnimationGroup * group = [CAAnimationGroup animation];
group.duration = 1;
group.removedOnCompletion = NO;
group.fillMode = @"forwards";
group.animations = @[basic1,basic2];
[self.animationView.layer addAnimation:group forKey:nil];
7、CADisplayLink 定时器
//CADisplayLink,selector每秒钟默认调用60次。
//CADisplayLink和NSTimer区别:CADisplayLink用来做绘图,重绘。NSTimer用于计时,重复调用。
//创建CADisplayLink
self.displaylink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleAction:)];
//调用次数 = 60 / frameInterval
self.displaylink.frameInterval = 3;
//将CADisplayLink放入RunLoop里
[self.displaylink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];