跟着大神一起进步,本篇博客原文地址:http://blog.csdn.net/hello_hwc?viewmode=contents
一 CALayer是什么?
Layers是绘图和动画的基础, Layer是在3D空间中的2D平面。Layer管理的几何(例如rotate,transfrom),内容(image等),和可视属性(backgroundColor,alpha)等信息。Layer主要通过管理bitmap来维护自己的状态信息,从这一点上来说,Layer可以看作对象模型,因为他们主要用来管理数据。
Layer是基于bitmap的,它会捕获View要呈现的内容,然后cache在一个bitmap中,这个bitmap可以看作一个对象。这样每次进行操作,例如平移旋转等,只是bitmap的矩阵运算。基于Layer的动画过程如图
由于基于Layer的绘制是处理静态的Bitmap的,而bitmap的处理又是GPU所擅长的,所以它的效率要比基于View绘制的高很多,因为基于View绘制的每次都要进行drawRect的调用重新绘制。
二 Layer支持继承 支持添加新的SubLayer 支持对sublayer进行层次调整
常见的Layer子类
管理Layer内容的几个函数
addSublayer:
insertSublayer:above:
insertSublayer:atIndex:
insertSublayer:below:
removeFromSuperlayer
replaceSublayer:with:
三 直接设置UIView 的Layer
先看一个示例,然后我们列出常用的属性,最后就是找几个比较不容易理解的属性单独分析
现在Stroyboard上拖拽一个UIView。然后control+drag出一个IBOutlet,命名为containView
@property (weak, nonatomic) IBOutlet UIView *containView;
在ViewDidLoad 中添加如下代码
self.containView.layer.backgroundColor = [UIColor lightGrayColor].CGColor;//背景颜色 使用CGColor
self.containView.layer.cornerRadius = 20.0;//圆角
self.containView.layer.shadowColor = [UIColor blueColor].CGColor;//阴影颜色
self.containView.layer.shadowOpacity = 0.8;//阴影透明度
self.containView.layer.shadowOffset = CGSizeMake(3.0, 3.0);
self.containView.layer.borderWidth = 2.0f;//设置边界的宽度
self.containView.layer.borderColor = [UIColor redColor].CGColor;//边界颜色
这样,运行后的效果
四 添加SubLayer
CALayer *subLayer1 = [CALayer layer];
subLayer1.frame = CGRectMake(, , , );
subLayer1.backgroundColor = [UIColor blueColor].CGColor;
subLayer1.anchorPoint = CGPointMake(0.5, 0.5);
subLayer1.position = CGPointMake(, );
[self.containView.layer addSublayer:subLayer1];
效果图如图
有可能添加SubLayer的时候,sublayer的frame范围已经超过了super Layer的frame,那么会怎么样呢?
sublayer1.position = CGPointMake(,CGRectGetMaxY(containView.bounds)-);
但是很多时候我们并不想subLayer的范围超出super layer 这个时候可以设置这个属性
self.containView.layer.masksToBounds = YES;
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAN4AAADSCAYAAAAlpX7MAAAKomlDQ1BJQ0MgUHJvZmlsZQAASImVlwdQFFkax193Tw6kgZHMEIcsOUqOQ5AcTQwzMAxhGAYGFbOyqOAaUBEBA8gSFVyVuAZEFNMioJh1QRYVdV0MmFDZBo7h9q7uru5f9VX/6uvX//f16/eqvgaAcpktFKbCMgCkCbJEoT7ujOiYWAZ+CEAAAURAAkw2J1PoFhwcAFDNXv+uD7fR0ahumkx5/fv9/ypZbkImBwAoGOV4biYnDeVTaBznCEVZACBcNK+9PEs4xVtQlhehBaJcNsW8GT4+xfEz3DU9JjzUA+W7ABAobLaIBwD5dzTPyObwUB8KBmUzAZcvQNkKZWdOEhudh4LeA8ZpaelTfBhl/fh/8uH9zTNe4slm8yQ88y7TInjyM4Wp7JX/53L8b6Wlimfn0EKDkiTyDZ2aD12zmpR0fwkL4hcGzTKfO1PTFCeJfSNmmZPpETvLXLan/yyLUyLcZpktmnuWn8UKn2VReqjEX5C6MEDin8CScEKmV9gsJ/K9WbOckxQeNcvZ/MiFs5yZEuY/N8ZDkheJQyU1J4q8Je+YljlXG4c9N1dWUrjvXA3Rknq4CZ5ekrwgQjJemOUu8RSmBs/Vn+ojyWdmh0mezUI32Cwns/2C53yCJesD+CAQsAEnK2HF1L4CHunClSI+LymL4YaekgQGS8AxNWZYmJnbAjB15mY+6Tv69FmC6FfnchkdANjno0neXI6tDUDbUwBoH+Zy2m/R7bATgDO9HLEoeyY3tdUBFj3H0kAeKAF1oA30gQmwADbAEbgCL+AHgkA4iAFLAQckgTQgAsvBarAB5IECsBPsBSXgEDgCasAxcAK0gNPgPLgEro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QvCxqjEEgp8B/AXt9ED5L2bV/AAAAAElFTkSuQmCC" alt="" />
这里再添加两个常用的CALayer 的子类UIShapeLayer 和 UITextLayer的示例
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, , );
CGPathAddLineToPoint(path, nil, , CGRectGetHeight(_containView.frame)/);
shapeLayer.path = path;
shapeLayer.bounds = CGRectMake(,,5.0,CGRectGetHeight(_containView.bounds)/);
shapeLayer.anchorPoint = CGPointMake(0.5, 0.5);
shapeLayer.position = CGPointMake(CGRectGetMidX(_containView.bounds),CGRectGetMidY(_containView.bounds));
shapeLayer.lineWidth = 5.0;
shapeLayer.lineCap = kCALineCapRound;
shapeLayer.strokeColor = [UIColor yellowColor].CGColor;
[self.containView.layer addSublayer:shapeLayer]; CATextLayer * textLayer = [CATextLayer layer];
NSString * text = @"blog.csdn.net/hello_hwc";
NSAttributedString * attributeString = [[NSAttributedString alloc] initWithString:text];
textLayer.string = text;
textLayer.alignmentMode = @"center";
textLayer.fontSize = ;
textLayer.foregroundColor = [UIColor brownColor].CGColor;
CGRect bounds;
bounds.origin = CGPointMake(, );
bounds.size = attributeString.size;
textLayer.bounds = bounds;
textLayer.position = CGPointMake(,);
[_containView.layer addSublayer:textLayer];
效果图如下
五 anchorPoint和position
和UIView不同,Layer主要由三个属性来设置位置(极少用Frame):
bounds - 设置大小
anchorPoint -设置锚点(锚点对后续的layer动画有很大影响)
position - 锚点在superLayer中的位置
这样说有点抽象,我们看看以下的图就了解了
对于iOS来说,坐标系如图,archPoint(x,y)的两个值通常取0.0-1.0,默认值是(0.5,0.5)这里的值可以看作所占用x的比例,比如默认的0.5,0.5就是在x的中间和y的中间。
而position则是AnchorPoint在super layer中的位置
如下图
六 layer 显示图片
CALayer * imageLayer = [CALayer layer];
imageLayer.bounds = CGRectMake(,,,);
imageLayer.position = CGPointMake(,);
imageLayer.contents = (id)[UIImage imageNamed:@"lichen.jpg"].CGImage;
imageLayer.contentsGravity = kCAGravityResizeAspect;
[containView.layer addSublayer:imageLayer];
效果图
这里,要详细讲解以下contentGravity这个属性。这个属性决定了contents如何填充。
具体分为两个方面,
方面一,位置方面
具体如图
方面二