iOS 离屏渲染 - 切圆角

只为探究日常开发过程中可能会出现的离屏渲染

本次实验仅探究使用cornerRadius & masksToBounds 切圆角

事先说明:查看离屏渲染是否产生通过模拟器中
查看离屏渲染设置 - 发生离屏渲染时背景颜色为黄色

1、切圆角是否一定会触发离屏渲染 - 多组对照实验(以下代码都可以拿去自己尝试,事实不会骗人)

    /// 第一组 不存在子view时
    /// 结论不会触发离屏渲染
    UIView *parentView = [[UIView alloc] init];
    parentView.layer.cornerRadius = 50;
    parentView.layer.masksToBounds = YES;
    parentView.frame = CGRectMake(0, 100, 100, 100);
    [self.view addSubview:parentView];
    /// 第二组存在子view时
    /// 结论不会触发离屏渲染
    UIView *parentView = [[UIView alloc] init];
    parentView.layer.cornerRadius = 50;
    parentView.layer.masksToBounds = YES;
    parentView.frame = CGRectMake(0, 100, 100, 100);
    [self.view addSubview:parentView];

    UIView *childrenView = [[UIView alloc] init];
    childrenView.frame = CGRectMake(0, 0, 40, 40);
    [parentView addSubview:childrenView];
    /// 第三组存在子view时,parentView存在背景色
    /// 结论不会触发离屏渲染
    UIView *parentView = [[UIView alloc] init];
    parentView.backgroundColor = [UIColor redColor];
    parentView.layer.cornerRadius = 50;
    parentView.layer.masksToBounds = YES;
    parentView.frame = CGRectMake(0, 100, 100, 100);
    [self.view addSubview:parentView];

    UIView *childrenView = [[UIView alloc] init];
    childrenView.frame = CGRectMake(0, 0, 40, 40);
    [parentView addSubview:childrenView];
    /// 第四组存在子view时,parentView和子view同时存在背景色
    /// 结论会触发离屏渲染
    UIView *parentView = [[UIView alloc] init];
    parentView.backgroundColor = [UIColor redColor];
    parentView.layer.cornerRadius = 50;
    parentView.layer.masksToBounds = YES;
    parentView.frame = CGRectMake(0, 100, 100, 100);
    [self.view addSubview:parentView];

    UIView *childrenView = [[UIView alloc] init];
    childrenView.frame = CGRectMake(0, 0, 40, 40);
    childrenView.backgroundColor = [UIColor blackColor];
    [parentView addSubview:childrenView];
    /// 第五组存在子view时,仅子View背景色存在,且被切割到
    /// 结论会触发离屏渲染
    UIView *parentView = [[UIView alloc] init];
    parentView.layer.cornerRadius = 50;
    parentView.layer.masksToBounds = YES;
    parentView.frame = CGRectMake(0, 100, 100, 100);
    [self.view addSubview:parentView];

    UIView *childrenView = [[UIView alloc] init];
    childrenView.frame = CGRectMake(0, 0, 40, 40);
    childrenView.backgroundColor = [UIColor blackColor];
    [parentView addSubview:childrenView];
    /// 第六组存在子view时,仅子View存在背景色且子View不被切割
    /// 结论不会触发离屏渲染
    UIView *parentView = [[UIView alloc] init];
    parentView.layer.cornerRadius = 50;
    parentView.layer.masksToBounds = YES;
    parentView.frame = CGRectMake(0, 100, 100, 100);
    [self.view addSubview:parentView];

    UIView *childrenView = [[UIView alloc] init];
    childrenView.frame = CGRectMake(25, 25, 40, 40);
    childrenView.backgroundColor = [UIColor blackColor];
    [parentView addSubview:childrenView];
    /// 第七组存在子view时,父view和子View同时存在背景色且子View不被切割
    /// 结论会触发离屏渲染
    UIView *parentView = [[UIView alloc] init];
    parentView.backgroundColor = [UIColor redColor];
    parentView.layer.cornerRadius = 50;
    parentView.layer.masksToBounds = YES;
    parentView.frame = CGRectMake(0, 100, 100, 100);
    [self.view addSubview:parentView];

    UIView *childrenView = [[UIView alloc] init];
    childrenView.frame = CGRectMake(25, 25, 40, 40);
    childrenView.backgroundColor = [UIColor blackColor];
    [parentView addSubview:childrenView];
    /// 第八组存在子view时,父view和子View同时存在背景色且相同,且子View不被切割
    /// 结论会触发离屏渲染
    UIView *parentView = [[UIView alloc] init];
    parentView.backgroundColor = [UIColor redColor];
    parentView.layer.cornerRadius = 50;
    parentView.layer.masksToBounds = YES;
    parentView.frame = CGRectMake(0, 100, 100, 100);
    [self.view addSubview:parentView];

    UIView *childrenView = [[UIView alloc] init];
    childrenView.frame = CGRectMake(25, 25, 40, 40);
    childrenView.backgroundColor = [UIColor redColor];
    [parentView addSubview:childrenView];

通过上述实验得到结论如下:
1、通过1、2组实验得出,存在子View并切割子View时不一定会出现离屏渲染
2、通过3、4、7、8组实验得出,存在子View且同时存在颜色时一定会出现离屏渲染
3、通过5、6组实验得出,仅子View存在背景颜色且被切割时一定会出现离屏渲染

总结:在不设置背景色时圆角放心大胆切,如果子View存在背景色仅一种情况不会出现离屏渲染,就是不切割到子View且父View不存在背景颜色

2、可能存在遗漏的实验组,辛苦大家留言区补充,尽量第一时间更新

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。