iOS开发中CALayer使用的基本教程

2020-01-14 17:52:10于丽

把剪切超出主图层部分的代码注释掉之后的显示效果

复制代码
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //设置边框的宽度为20
    self.customView.layer.borderWidth=5;
    //设置边框的颜色
    self.customView.layer.borderColor=[UIColor blackColor].CGColor;
    
    //设置layer的圆角
    self.customView.layer.cornerRadius=20;
    //设置超过子图层的部分裁减掉
    //UI框架中使用的方法
    //    self.customView.clipsToBounds=YES;
//    self.customView.layer.masksToBounds=YES;
    
    //在view的图层上添加一个image,contents表示接受内容
    self.customView.layer.contents=(id)[UIImage imageNamed:@"me"].CGImage;
    
    //设置阴影的颜色
    self.customView.layer.shadowColor=[UIColor blackColor].CGColor;
    //设置阴影的偏移量,如果为正数,则代表为往右边偏移
    self.customView.layer.shadowOffset=CGSizeMake(15, 5);
    //设置阴影的透明度(0~1之间,0表示完全透明)
    self.customView.layer.shadowOpacity=0.6;
}

 

iOS开发中CALayer使用的基本教程

5.只要继承自uiview的都有layer属性,下面以图片为例进行说明。

在storyboard中新添加一张图片。

iOS开发中CALayer使用的基本教程

简单设置示例

复制代码
#import "YYViewController.h"

 

@interface YYViewController ()