iOS CALayer应用详解

跟着大神一起进步,本篇博客原文地址: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(0, 0, 80, 80);
    subLayer1.backgroundColor = [UIColor blueColor].CGColor;
    subLayer1.anchorPoint = CGPointMake(0.5, 0.5);
    subLayer1.position = CGPointMake(100, 100);
    [self.containView.layer addSublayer:subLayer1];

效果图如图

有可能添加SubLayer的时候,sublayer的frame范围已经超过了super Layer的frame,那么会怎么样呢?

 

sublayer1.position = CGPointMake(0,CGRectGetMaxY(containView.bounds)-10);  

但是很多时候我们并不想subLayer的范围超出super layer 这个时候可以设置这个属性

self.containView.layer.masksToBounds = YES;  

 

这里再添加两个常用的CALayer 的子类UIShapeLayer 和 UITextLayer的示例

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, nil, 0, 0);
    CGPathAddLineToPoint(path, nil, 0, CGRectGetHeight(_containView.frame)/2);
    shapeLayer.path = path;
    shapeLayer.bounds = CGRectMake(0,0,5.0,CGRectGetHeight(_containView.bounds)/2);
    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 = 12;
    textLayer.foregroundColor = [UIColor brownColor].CGColor;
    CGRect bounds;
    bounds.origin = CGPointMake(0, 0);
    bounds.size = attributeString.size;
    textLayer.bounds = bounds;
    textLayer.position = CGPointMake(100,100);
    [_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(0,0,200,100);  
imageLayer.position = CGPointMake(200,200);  
imageLayer.contents = (id)[UIImage imageNamed:@"lichen.jpg"].CGImage;  
imageLayer.contentsGravity = kCAGravityResizeAspect;  
[containView.layer addSublayer:imageLayer]; 

效果图

这里,要详细讲解以下contentGravity这个属性。这个属性决定了contents如何填充。
具体分为两个方面,
方面一,位置方面
具体如图

方面二

 

posted @ 2017-01-03 14:01  幻影-2000  阅读(352)  评论(0编辑  收藏  举报