CALayer基本使用

CALayer基本使用

通过下面传统上的创建UIView和CALayer这两个的创建和使用我们可以看出来其实CALayer无论是创建还是使用都和UIVIew比较相似, 但是需要注意的是,UIVIew最后添加到的self.view 而CALayer最后添加到的是self.view.layer 其次,CALayer在使用颜色设置、添加子视图等的时候都需要将其转换成VIew型的,不能直接添加。

//这个是传统意义上的创建UIView
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 100, 330, 200)];

//设置VIew上的背景颜色                         
view.backgroundColor = [UIColor redColor];

//设置View上layer的背景颜色,需要将其转换成View型的背景颜色                                     
view.layer.backgroundColor = [UIColor orangeColor].CGColor;

//设置边框的宽度
view.layer.borderWidth = 3;

//设置边框颜色 
view.layer.borderColor = [UIColor blackColor].CGColor;

//设置边框的倒角弧度
view.layer.cornerRadius = 20;
  • ** 注意:以上的信息不是只有CALayer才能设置的,VIew上也可以自己直接设置,但是VIew其实还是基于CALayer上搭建起来的视图,只是这个视图层上是可以添加触发事件的**

CALayer的基础

  • 对CALayer进行基本的设置
    CALayer *layer = [CALayer layer];
    // layer.frame = CGRectMake(0, 100, 350, 200);
    layer.backgroundColor = [UIColor orangeColor].CGColor;
    
    // 1、bounds: 尺寸
    layer.bounds = CGRectMake(0, 0, 200, 200);
    // 2、position: 定位点
    layer.position = self.view.center;
    
    // 3、锚点、支点:决定layer上的哪个点在 position 点上,默认(0.5, 0.5),范围:(0,0) ~ (1,1)
    layer.anchorPoint = CGPointMake(0.5, 0.5);
    
    // 4、z方向的层级
    layer.zPosition = 2;
    
    // 5、设置圆角:cornerRadius
    layer.cornerRadius = 100;
    
    // 6、填充内容
    // layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"image6.jpg"].CGImage);
    
    layer.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"juhua"]].CGColor;
    
    // 7、是否可以裁剪多余的图层
    // layer.masksToBounds = YES;
    
    // 8、设置边框宽度和颜色
    layer.borderWidth = 5;
    layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    // 9、设置阴影: 尺寸、颜色、透明度、圆角
    layer.shadowOffset = CGSizeMake(10, 5);
    layer.shadowColor = [UIColor redColor].CGColor;
    layer.shadowOpacity = 0.5;
    layer.shadowRadius = 10;
    
  • 1、创建layer并填充图片

    1、创建layer
    CALayer *layer = [[CALayer alloc] init];
    layer.frame = CGRectMake(10, 320, 330, 200);
    layer.backgroundColor = [UIColor blueColor].CGColor;
    
    2、将图层添加到父图层
    [self.view.layer addSublayer:layer];
    
    3、填充图片内容,需要将 UIImage 桥接(____bridge)到CGImage
    layer.contents = (__bridge id )([UIImage imageNamed:@"image4.jpg"].CGImage);
    
    4、使用layer CATextLayer 子类填充文字(CATextLayer是基于CALayer上的子类)
    CATextLayer *textLayer = [[CATextLayer alloc] init];
    textLayer.frame = CGRectMake(10, 550, 300, 30);
    textLayer.string = @"这是layer填充的文字内容";

    2、设置CATextLayer

    设置CATextLayer在没有显示字体之前的颜色
    textLayer.foregroundColor = [UIColor blackColor].CGColor;
    
    设置CATextLayer 的背景颜色
    textLayer.backgroundColor = [UIColor redColor].CGColor;
    
    设置显示的字体高度和需要开辟显示字体的宽度        
    textLayer.font = (__bridge CFTypeRef _Nullable)([UIFont systemFontOfSize:20 weight:500]);
    
    设置显示的文字高度
    textLayer.fontSize = 20;
    
    设置文字对齐方式
    textLayer.alignmentMode = @"center";textLayer.truncationMode = @"middle";
    
    将图层添加到父图层
    [self.view.layer addSublayer:textLayer]; 

转载于:https://my.oschina.net/xiaoyaner0708/blog/738481

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值