• IOS 自定义Layer(图层)


    方式1:
    
    
    @interface NJViewController ()
    
    @end
    
    @implementation NJViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // 如果一个控制是另外一个控件的子控件, 那么这个控件中的layer也是另外一个控件的子layer
    //     NSLog(@"star - %@", self.view.layer.sublayers);
        CALayer *layer = [CALayer layer];
        layer.backgroundColor = [UIColor redColor].CGColor;
        layer.bounds = CGRectMake(0, 0, 100, 100);
    //    layer.position = CGPointMake(200, 200);
    //    layer.contents = (id)[UIImage imageNamed:@"me"].CGImage;
        [self.view.layer addSublayer:layer];
    
        
    }
    
    - (void)test
    {
        
        NSLog(@"star - %@", self.view.layer.sublayers);
        
        // 1.创建layer
        //    CALayer *layer = [[CALayer alloc] init];
        CALayer *layer = [CALayer layer];
        layer.backgroundColor = [UIColor redColor].CGColor;
        layer.bounds = CGRectMake(0, 0, 100, 100);
        layer.position = CGPointMake(200, 200);
        layer.borderWidth = 10;
        layer.cornerRadius = 10;
        // 将layer添加在界面上
        [self.view.layer addSublayer:layer];
        
        //    NSLog(@"%@", layer.superlayer); // 获取layer的父视图
        NSLog(@"end - %@", self.view.layer.sublayers);
        
        
        //
        //    UIView *view = [[UIView alloc] init];
        //    view.superview;
        //    view.subviews;
        //    [self.view addSubview:view];
    }

     方式2:

    NJLayer.h / .m

    @implementation NJLayer
    
    // 重写该方法, 在该方法中给layer上绘制图形
    // 注意CALayer中的drawInContext方法, 不会自动调用
    // 只能自己通过setNeedDisplay方法调用
    - (void)drawInContext:(CGContextRef)ctx
    {
    
        // 1.绘制图形
        CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 100, 100));
        
    //    [[UIColor redColor] set]; // 注意不能用UIKit框架中的类
        
        CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
        // 1.渲染图形
        CGContextFillPath(ctx);
    }
    
    @end
    View Code

    调用

    #import "NJLayer.h"
    
    @interface NJViewController ()
    
    @end
    
    @implementation NJViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // 1.创建自定义Layer
        CALayer *myLayer = [CALayer layer];
        myLayer.bounds = CGRectMake(0, 0, 100, 100);
        myLayer.anchorPoint = CGPointZero;
        myLayer.backgroundColor = [UIColor greenColor].CGColor;
        
        myLayer.delegate = self;
        
        // 1.1手动调用CALayer中的SETNEEDDISPLAY方法绘制图片
        [myLayer setNeedsDisplay];
        
        // 2.将自定义Layer添加到控制器的view的layer上
        [self.view.layer addSublayer:myLayer];
    }
    
    // 通过代理自定义layer
    - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
    {
        
        // 1.绘制图形
        CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 50, 100));
    
        CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
        // 1.渲染图形
        CGContextFillPath(ctx);
    }
    
    
    - (void)test
    {
        // 1.创建自定义Layer
        NJLayer *myLayer = [NJLayer layer];
        
        myLayer.bounds = CGRectMake(0, 0, 100, 100);
        myLayer.anchorPoint = CGPointZero;
        myLayer.backgroundColor = [UIColor greenColor].CGColor;
        //    myLayer.position = CGPointMake(200, 200);
        
        // 1.1手动调用CALayer中的SETNEEDDISPLAY方法绘制图片
        [myLayer setNeedsDisplay];
        
        // 2.将自定义Layer添加到控制器的view的layer上
        [self.view.layer addSublayer:myLayer];
    }
    View Code
  • 相关阅读:
    CSU 1505: 酷酷的单词【字符串】
    HDU 2036 改革春风吹满地【计算几何/叉乘求多边形面积】
    HDU 2034 人见人爱A-B【STL/set】
    HDU 2031 进制转换
    HDU 1020 Encoding【连续的计数器重置】
    HDU 1999 不可摸数【类似筛法求真因子和】
    动态规划总结
    CSU 1785: 又一道简单题
    CSU 1779: 错误的算法【矩阵/模拟】
    CSU 1777: 大还是小?【模拟/后导0】
  • 原文地址:https://www.cnblogs.com/liuwj/p/6599329.html
Copyright © 2020-2023  润新知