只为探究日常开发过程中可能会出现的离屏渲染
本次实验仅探究使用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存在背景颜色且被切割时一定会出现离屏渲染