CoreAnimation之CALayer基础
CoreAnimation之变换
1. CAShapeLayer
CAShapeLayer是一个通过矢量图形而不是bitmap来绘制的图层子类,使用CAShapeLayer有以下一些优点:
渲染快速——CAShapeLayer使用了硬件加速,绘制同一图形会比用Core Graphics快很多
高效使用内存——一个CAShapeLayer不需要像普通CALayer一样创建一个寄宿图形,所以无论有多大,都不会占用太多的内存
不会被图层边界剪裁掉——一个CAShapeLayer可以在边界之外绘制。你的图层路径不会像在使用Core Graphics的普通CALayer一样被剪裁掉
不会出现像素化——当你给CAShapeLayer做3D变换时,它不像一个有寄宿图的普通图层一样变得像素化
CAShapeLayer可以用来绘制所有能够通过CGPath来表示的形状。这个形状不一定要闭合,图层路径也不一定要不可破,事实上你可以在一个图层上绘制好几个不同的形状。你可以控制一些属性比如lineWith,lineCap,和lineJoin。但是在图层层面你只有一次机会设置这些属性,如果你想用不同颜色或风格来绘制多个形状,就不得不为每个形状准备一个图层了。
说了这么多,CAShapeLayer到底能用来做什么呢?
-
CAShapeLayer实现视图的部分圆角:
-(void)drawCorner{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
view.center = self.view.center;
view.backgroundColor = [UIColor blackColor];
[self.view addSubview:view];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:view.frame byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(30.0f, 30.0f)];
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
layer.bounds = view.frame;
layer.position = CGPointMake(50.0f, 50.0f);
layer.path = path.CGPath;
view.layer.mask = layer;
}
运行效果:
-
CAShapeLayer实现一个呆萌的火柴人:
-(void)drawMatchman{
CGFloat radius = 25.0f; //半径
UIBezierPath *path = [[UIBezierPath alloc] init];
CGPoint point1 = CGPointMake(self.view.center.x + radius, self.view.center.y);
[path moveToPoint:point1]; //将画笔移动到point1
[path addArcWithCenter:self.view.center radius:radius startAngle:0.0f endAngle:2.0f*M_PI clockwise:YES]; //画一个圆代表火柴人的头
CGPoint point2 = CGPointMake(point1.x - radius, point1.y + radius);
[path moveToPoint:point2]; //将画笔移动到point2,准备画身体
CGPoint point3 = CGPointMake(point2.x, point2.y+50.0f);
[path addLineToPoint:point3]; //画一根长为50的竖线代表火柴人的身体,起点是point2,终点是point3
CGPoint point4 = CGPointMake(point3.x - radius, point3.y+25.0f);
[path addLineToPoint:point4]; //画一根长为25的左斜线代表火柴人的左脚,起点是point3,终点是point4
[path moveToPoint:point3]; //将画笔移动到point3,准备画右脚
CGPoint point5 = CGPointMake(point3.x + radius, point3.y+25.0f);
[path addLineToPoint:point5]; //画一根长为25的右斜线代表火柴人的右脚,起点是point3,终点是point5
//最后画一根横线,代表火柴人的手
CGPoint point6 = CGPointMake(point2.x - radius, point2.y + 25.0f);
[path moveToPoint:point6];
[path addLineToPoint:CGPointMake(point6.x + 50.0f, point6.y)];
shapeLayer = [CAShapeLayer layer];
shapeLayer.strokeColor = [UIColor redColor].CGColor; //画笔颜色
shapeLayer.fillColor = [UIColor clearColor].CGColor; //填充色
shapeLayer.lineWidth = 6.0f; //线条宽度
shapeLayer.lineJoin = kCALineJoinRound; //线条连接处的样式
shapeLayer.lineCap = kCALineCapRound; //线条末端处的样式
shapeLayer.path = path.CGPath;
[self.view.layer addSublayer:shapeLayer];
[self addFlagPoint:point1];
[self addFlagPoint:point2];
[self addFlagPoint:point3];
[self addFlagPoint:point4];
[self addFlagPoint:point5];
[self addFlagPoint:point6];
}
-(void)addFlagPoint:(CGPoint)aPoint{
UIView *flag = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 6.0f, 6.0f)];
flag.center = aPoint;
flag.layer.cornerRadius = 3.0f;
flag.backgroundColor = [UIColor blackColor];
[self.view addSubview:flag];
}
运行效果:
-
CAShapeLayer实现一个扇形动画:
-(void)drawCircular{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
view.center = self.view.center;
UIImage *image = [UIImage imageNamed:@"3"];
view.layer.contents = (__bridge id _Nullable)(image.CGImage);
view.layer.contentsGravity = kCAGravityCenter;
view.layer.contentsScale = [UIScreen mainScreen].scale;
[self.view addSubview:view];
shapeLayer = [CAShapeLayer layer];
shapeLayer.frame = view.bounds;
shapeLayer.strokeEnd = 0.0f;
shapeLayer.strokeStart = 0.0f;
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:view.bounds];
shapeLayer.path = path.CGPath;
shapeLayer.fillColor = [UIColor clearColor].CGColor;
shapeLayer.lineWidth = 100.0f;
shapeLayer.strokeColor = [UIColor redColor].CGColor;
view.layer.mask = shapeLayer;
[NSTimer scheduledTimerWithTimeInterval:0.005f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
}
-(void)timerAction{
static BOOL flag = NO;
if (shapeLayer.strokeEnd >= 1.5f) {
flag = YES;
}
if (shapeLayer.strokeEnd <= -0.5f) {
flag = NO;
}
if (flag) {
shapeLayer.strokeEnd -= 0.005f;
}else{
shapeLayer.strokeEnd += 0.005f;
}
}
运行效果:
更多关于CAShapeLayer的动画:动画黄金搭档:CADisplayLink & CAShapeLayer
2. CATransformLayer
之前我们在CoreAnimation之变换中构造了一个残缺的正方体,最后在旋转正方体的时候遇到了问题,原因在于CALayer是扁平的,所以直接将superLayer绕y轴旋转的时候看不出正方体的3D效果
CoreAnimation有一个专用图层叫CATransformLayer,它是CALayer的子类,但是不同于普通的CALayer,因为它不能显示它自己的内容,只有当存在了一个能作用域子图层的变换它才真正存在,而且CATransformLayer并不平面化它的子图层,所以它能够用于构造一个层级的3D结构
总之一句话,CATransformLayer相当于一个容器,一个3D的容器
这次我们依然以构建一个正方体为例,开始写代码前,我们不妨在脑袋里先构造一下这个正方体:
首先,有六块木板,都是平放在一个3D空间里
第一步,构建上下两面,把上面这块木板往上移动50个点(即沿z轴移动50个点);再把下面这块木板往下移动50个点(即沿z轴移动-50个点),这样就构建出了上下两面
第二步,构建左右两面,将左面这块木板往左边直立起来(即沿y轴旋转-90度),再将左面这块木板往上移动50个点(即沿z轴移动50个点);同理,右面这块木板就是先往右边直立起来再往上移动
第三步,构建前后两面,将前面这块木板往前边直立起来(即沿x轴旋转-90度),再将前面这块木板往上移动50个点(即沿z轴移动50个点);同理,后面这块木板就是先往后边直立起来再往上移动
代码如下(楼主也是初学,为了逻辑清晰,就没有封装方法,直接一个面一个方法,所以代码有点多哈):
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController{
CATransformLayer *transformLayer;
NSTimer *timer;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CATransform3D sublayerTransform = CATransform3DIdentity;
sublayerTransform.m34 = -1.0f/500.0f;
self.view.layer.sublayerTransform = sublayerTransform;
transformLayer = [CATransformLayer layer];
transformLayer.position = self.view.layer.position;
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DRotate(transform, -M_PI_4, 1.0f, 0.0f, 0.0f);
transform = CATransform3DRotate(transform, -M_PI_4, 0.0f, 1.0f, 0.0f);
transformLayer.transform = transform;
[self.view.layer addSublayer:transformLayer];
[self addFace1];
[self addFace2];
[self addFace3];
[self addFace4];
[self addFace5];
[self addFace6];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[timer invalidate];
timer = nil;
timer = [NSTimer scheduledTimerWithTimeInterval:0.025f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
}
-(void)timerAction{
CATransform3D transform = transformLayer.transform;
transform = CATransform3DRotate(transform, 1.0f/180.0f*M_PI, 1.0f, 1.0f, 0.0f);
transformLayer.transform = transform;
}
//上面 ———— 把上面这块木板往上移动50个点(即沿z轴移动50个点)
-(void)addFace1{
CALayer *layer = [[CALayer alloc] init];
layer.bounds = CGRectMake(0.0f, 0.0f, 100.0f, 100.0f);
layer.backgroundColor = [UIColor redColor].CGColor;
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DTranslate(transform, 0.0f, 0.0f, 50.0f);
layer.transform = transform;
[transformLayer addSublayer:layer];
}
//下面 ———— 把下面这块木板往下移动50个点(即沿z轴移动-50个点)
-(void)addFace2{
CALayer *layer = [[CALayer alloc] init];
layer.bounds = CGRectMake(0.0f, 0.0f, 100.0f, 100.0f);
layer.backgroundColor = [UIColor orangeColor].CGColor;
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DTranslate(transform, 0.0f, 0.0f, -50.0f);
layer.transform = transform;
[transformLayer addSublayer:layer];
}
//左面
-(void)addFace3{
CALayer *layer = [[CALayer alloc] init];
layer.bounds = CGRectMake(0.0f, 0.0f, 100.0f, 100.0f);
layer.backgroundColor = [UIColor yellowColor].CGColor;
CATransform3D transform = CATransform3DIdentity;
//将左面这块木板往左边直立起来(即沿y轴旋转-90度)
transform = CATransform3DRotate(transform, -M_PI_2, 0.0f, 1.0f, 0.0f);
//再将左面这块木板往上移动50个点(即沿z轴移动50个点)
transform = CATransform3DTranslate(transform, 0.0f, 0.0f, 50.0f);
layer.transform = transform;
[transformLayer addSublayer:layer];
}
//右面
-(void)addFace4{
CALayer *layer = [[CALayer alloc] init];
layer.bounds = CGRectMake(0.0f, 0.0f, 100.0f, 100.0f);
layer.backgroundColor = [UIColor greenColor].CGColor;
CATransform3D transform = CATransform3DIdentity;
//将右面这块木板往右边直立起来(即沿y轴旋转90度)
transform = CATransform3DRotate(transform, M_PI_2, 0.0f, 1.0f, 0.0f);
//再将右面这块木板往上移动50个点(即沿z轴移动50个点)
transform = CATransform3DTranslate(transform, 0.0f, 0.0f, 50.0f);
layer.transform = transform;
[transformLayer addSublayer:layer];
}
//前面
-(void)addFace5{
CALayer *layer = [[CALayer alloc] init];
layer.bounds = CGRectMake(0.0f, 0.0f, 100.0f, 100.0f);
layer.backgroundColor = [UIColor blueColor].CGColor;
CATransform3D transform = CATransform3DIdentity;
//将前面这块木板往前边直立起来(即沿x轴旋转-90度)
transform = CATransform3DRotate(transform, -M_PI_2, 1.0f, 0.0f, 0.0f);
//再将前面这块木板往上移动50个点(即沿z轴移动50个点)
transform = CATransform3DTranslate(transform, 0.0f, 0.0f, 50.0f);
layer.transform = transform;
[transformLayer addSublayer:layer];
}
//后面
-(void)addFace6{
CALayer *layer = [[CALayer alloc] init];
layer.bounds = CGRectMake(0.0f, 0.0f, 100.0f, 100.0f);
layer.backgroundColor = [UIColor purpleColor].CGColor;
CATransform3D transform = CATransform3DIdentity;
//将后面这块木板往后边直立起来(即沿x轴旋转90度)
transform = CATransform3DRotate(transform, M_PI_2, 1.0f, 0.0f, 0.0f);
//再将后面这块木板往上移动50个点(即沿z轴移动50个点)
transform = CATransform3DTranslate(transform, 0.0f, 0.0f, 50.0f);
layer.transform = transform;
[transformLayer addSublayer:layer];
}
@end
以前在CALayer上旋转正方体的时候我们要这样写(即把所有的子图层挨个儿做一次变换):
-(void)timerAction{
static CGFloat angle = 1.0f;
CATransform3D transform3d = self.containerView.layer.sublayerTransform;
transform3d = CATransform3DRotate(transform3d, angle/180.0f*M_PI, 0.0f, 1.0f, 0.0f);
self.containerView.layer.sublayerTransform = transform3d;
}
而现在在CATransformLayer上旋转正方体,只需要将CATransformLayer绕x轴或者绕y轴旋转就行了:
-(void)timerAction{
CATransform3D transform = transformLayer.transform;
transform = CATransform3DRotate(transform, 1.0f/180.0f*M_PI, 1.0f, 1.0f, 0.0f);
transformLayer.transform = transform;
}
运行效果:
3. CAGradientLayer
CAGradientLayer可以用来实现渐变效果:
CAGradientLayer *layer = [CAGradientLayer layer];
layer.bounds = CGRectMake(0.0f, 0.0f, 150.0f, 150.0f);
layer.position = self.view.layer.position;
layer.colors = @[(__bridge id)[UIColor redColor].CGColor,(__bridge id)[UIColor greenColor].CGColor,(__bridge id)[UIColor blueColor].CGColor];
layer.locations = @[@.25,@0.5,@0.75];
layer.startPoint = CGPointMake(0.0f, 0.0f);
layer.endPoint = CGPointMake(1.0f, 0.0f);
[self.view.layer addSublayer:layer];
运行效果:
需要特别说明一下locations这个属性,locations数组里面装的是相对位置,这个相对位置必须是单调递增的,但是这个位置并不是代表颜色的位置,而是说从这个位置开始,将要开始渐变成下一个颜色了
拿示例代码来说,从0.25开始,将要由红变绿了,从0.5开始将要由绿变蓝了,从0.75开始又要开始下一个渐变了,但是由于没有下一个颜色了,所以后面全是蓝色,你可以在示例代码的colors里面再添加一个颜色试试
4. CAReplicatorLayer
学习CAReplicatorLayer之前,我们再来复习一下变换的顺序:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController{
CALayer *layer;
}
- (void)viewDidLoad {
[super viewDidLoad];
layer = [CALayer layer];
layer.backgroundColor = [UIColor cyanColor].CGColor;
UIImage *image = [UIImage imageNamed:@"3"];
layer.frame = CGRectMake(110.0f, 100.0f, 100.0f, 100.0f);
layer.contents = (__bridge id)image.CGImage;
layer.contentsGravity = kCAGravityResizeAspect;
layer.contentsScale = [UIScreen mainScreen].scale;
[self.view.layer addSublayer:layer];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self rotate];
}
-(void)rotate{
__weak __typeof__(self) weakSelf = self;
[UIView animateWithDuration:3.0f animations:^{
CATransform3D transform = layer.transform;
transform = CATransform3DRotate(transform, M_PI, 0.0f, 0.0f, 1.0f);
layer.transform = transform;
} completion:^(BOOL finished) {
[weakSelf translate];
}];
}
-(void)translate{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:3.0f animations:^{
CATransform3D transform = layer.transform;
transform = CATransform3DTranslate(transform, 0.0f, 100.0f, 0.0f);
layer.transform = transform;
}];
});
}
@end
运行效果
注意这句代码:
transform = CATransform3DTranslate(transform, 0.0f, 100.0f, 0.0f);
明明是沿y轴移动200个点,为啥运行的时候会往上跑呢❓
原因在于,在移动之前,我们已经绕z轴旋转过了:
transform = CATransform3DRotate(transform, M_PI, 0.0f, 0.0f, 1.0f);
旋转之后的layer,其相对于superLayer的坐标系已经发生了改变
拿示例代码来说,绕z轴旋转180度之后,x轴和y轴的方向都已经变成与原来相反的方向了
变换的顺序,在CAReplicatorLayer中体现得尤为明显,因为CAReplicatorLayer的instance的变换是逐步增加的,每个实例都是相对于前一实例布局
CAReplicatorLayer实现一个带倒影的ImageView:
#import "ReplicatorImageView.h"
@implementation ReplicatorImageView
+ (Class)layerClass{
return [CAReplicatorLayer class];
}
- (void)setUp{
CAReplicatorLayer *layer = (CAReplicatorLayer *)self.layer;
layer.instanceCount = 2;
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DScale(transform, 1, -1, 0);
transform = CATransform3DTranslate(transform, 0.0f, -self.frame.size.height, 0.0f);
layer.instanceTransform = transform;
layer.instanceAlphaOffset = -0.6;
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = layer.bounds;
imageLayer.contents = (__bridge id _Nullable)(self.image.CGImage);
imageLayer.contentsScale = [UIScreen mainScreen].scale;
imageLayer.contentsGravity = kCAGravityResizeAspect;
[layer addSublayer:imageLayer];
}
-(void)setReplicatorImage:(UIImage *)replicatorImage{
self.image = replicatorImage;
[self setUp];
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
ReplicatorImageView *imageView = [[ReplicatorImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 150.0f)];
imageView.center = self.view.center;
[self.view addSubview:imageView];
UIImage *image = [UIImage imageNamed:@"3"];
imageView.replicatorImage = image;
}
运行效果:
instanceCount指定了总共要复制多少个图层(包含本身)
instanceAlphaOffset = -0.6f; 即当前图层实例的alpha值 = 上一个图层实例的alpha - 0.6f,与之类似的属性还有这些:
/* The color components added to the color of instance k-1 to produce
* the modulation color of instance k. Defaults to the clear color (no
* change). Animatable. */
@property float instanceRedOffset;
@property float instanceGreenOffset;
@property float instanceBlueOffset;
更多CAReplicatorLayer动画:基于CAReplicatorLayer的炫酷动画