iOS CALayer简单学习

来源:互联网 发布:五线谱识谱软件 编辑:程序博客网 时间:2024/05/29 15:20

    最近突然想起写一点博客 今天写一点关于iOS图层的知识
在iOS中所有的View都是都自带一个layer View的所有内容也都是通过layer显现出来的 layer与图层的区别就是layer不能相应用户点击事件,而View可以响应用户点击事件,但layer更加轻量级,更加节省空间和迅速 下面我们就看一下吧
layer有几个比较重要的属性  :
 transform   anchorPoint  position

创建一个layer
    CALayer * layer = [[CALayer alloc]init] ;    //CALayer * layer  = [CALayer layer] ;        // 边框宽度    layer.borderWidth = 10;    // 边框颜色    layer.borderColor = [UIColor greenColor].CGColor;    // 圆角    layer.cornerRadius = 10;        //设置是否按照边界裁剪,如果设为yes,阴影就不能显现相出来    //    layer.masksToBounds = YES;    // 阴影颜色    layer.shadowColor = [UIColor blueColor].CGColor;    // 阴影偏差    layer.shadowOffset = CGSizeMake(20, 20);    // 阴影不透明度    layer.shadowOpacity = 0.5;
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">layer.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">contents</span> = (<span style="font-variant-ligatures: no-common-ligatures; color: #c32275">id</span>)[<span style="font-variant-ligatures: no-common-ligatures; color: #6122ae">UIImage</span> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">imageNamed</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">@"lufy"</span>].<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">CGImage</span>;</p>


图层的transform属性
//layer 的3D缩放比例    layer.transform = CATransform3DMakeScale(1.5, 0.5, 0);    //图层的旋转    layer.transform = CATransform3DMakeRotation(M_PI_4, 0, 0, 1);    //下面是用kvc 的方法设置图层的变换属性    NSValue *value = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, 1)];    [layer setValue:value forKeyPath:@"transform"];        [layer setValue:@(M_PI_2) forKeyPath:@"transform.rotation"];            layer.transform = CATransform3DMakeScale(0.5, 2, 0);    [layer setValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 2, 0)] forKeyPath:@"transform"];        // 可以传递哪些key path, 在官方文档搜索 "CATransform3D key paths"    [layer setValue:@(-100) forKeyPath:@"transform.translation.x"];
position 就是图层在父视图中的位置 默认为CGPointZero

    layer.position =CGPointZero;

anchorPoint是layer的抛锚点,坐标系是自己的坐标系 是把自己的那个点放在设置的position 上 默认为CGPointZero 并且如果设置,x和y的值都是0——1 之间 。

    layer.anchorPoint =CGPointZero;



layer的隐式动画
    layer.backgroundColor = [UIColor blueColor].CGColor;    [CATransaction begin]; // 开启事务    //关闭隐式动画  默认是不关闭 也就是NO    [CATransaction setDisableActions:NO];        self.layer.position = CGPointMake(100, 100);    self.layer.opacity = 0.5;        [CATransaction commit]; // 提交事务

自定义layer
自定义layer是为了实现自己想要的一些效果,比如在图层上画圆,画线等一些我们自己特别定制的效果
这个时候我们需要创建一个类继承与CALayer 但是我们需要在ViewController中从新渲染
ViewController中的代码

   

    MyLayer *layer = [MyLayer layer];    layer.bounds = CGRectMake(0, 0, 100, 100);    layer.backgroundColor = [UIColor blueColor].CGColor;    layer.anchorPoint = CGPointZero;    [layer setNeedsDisplay];    [self.view.layer addSublayer:layer];

自定义的layer类中需要从写方法

- (void)drawInContext:(CGContextRef)ctx


/** *  只有明显地调用setNeedsDisplay方法,才会调用drawInContext:方法进行绘制 */- (void)drawInContext:(CGContextRef)ctx{    // 红色    CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);    // 添加圆    CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 50, 50));    // 实心绘制    CGContextFillPath(ctx);}

另外我们还可以设置代理,在这里我有必要说明一下图层设置代理  

图层设置代理的时候不必遵守任何协议 图层的代理应该设置为自己的View,自己的View的图层又是自己 例如UIImageView的对象imgV的layer的代理应该设置为imgV,不能设置为其他


#pragma mark - 图层的代理方法- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{    CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);    CGContextAddRect(ctx, CGRectMake(0, 0, 20, 20));    CGContextFillPath(ctx);}

 希望这篇文章能够对你起到帮助








0 0
原创粉丝点击