iOS 黑白屏滤镜视图层级穿透原理

添加灰色滤镜方法

@implementation TPBlackWhiteScreenTool

+ (void)addGrayFilterToView:(UIView *)view {
    if (@available(iOS 13.0,*)) {
        [view.kGrayView removeFromSuperview];
        UIView *greyView = [[UIView alloc] initWithFrame:view.bounds];
        greyView.userInteractionEnabled = NO;
        greyView.backgroundColor = [UIColor lightGrayColor];
        greyView.layer.compositingFilter = @"saturationBlendMode";
        greyView.layer.zPosition = FLT_MAX;
        [view addSubview:greyView];
        view.kGrayView = greyView;
    }
}

调用

情况 一

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:view];
    view.backgroundColor = [UIColor clearColor];
    [TPBlackWhiteScreenTool addGrayFilterToView:view];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    view2.backgroundColor = [UIColor clearColor];
    [view addSubview:view2];
    
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(100, 300, 100, 100)];
    view3.backgroundColor = [UIColor whiteColor];
    [view addSubview:view3];
}

效果图
请添加图片描述

view 是透明色,滤镜添加到 view上面,view 被穿透了
view2 由于是透明色,被灰色滤镜穿透了,view3是白色,没有被穿透

情况二

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:view];
    view.backgroundColor = [UIColor whiteColor];
    [TPBlackWhiteScreenTool addGrayFilterToView:view];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    view2.backgroundColor = [UIColor clearColor];
    [view addSubview:view2];
    
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(100, 300, 100, 100)];
    view3.backgroundColor = [UIColor whiteColor];
    [view addSubview:view3];
}

效果图 请添加图片描述
由于这时候view的背景色是白色,就不会穿透了,所以完全成了白色

情况三

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [TPBlackWhiteScreenTool addGrayFilterToView:self.view];

    UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:view];
    view.backgroundColor = [UIColor clearColor];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    view2.backgroundColor = [UIColor clearColor];
    [view addSubview:view2];
    
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(100, 300, 100, 100)];
    view3.backgroundColor = [UIColor whiteColor];
    [view addSubview:view3];
}

效果图 ,和第二种情况是一样的,因为滤镜视图添加到了self.view上面,并且self.view 白色的,滤镜的灰色就不会穿透
![请添加图片描述](https://img-blog.csdnimg.cn/9ad4af6ee04f4f46a4f3a3c68fe603c4.png = x600)

情况四

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    [TPBlackWhiteScreenTool addGrayFilterToView:self.view];

    UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:view];
    view.backgroundColor = [UIColor clearColor];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    view2.backgroundColor = [UIColor clearColor];
    [view addSubview:view2];
    
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(100, 300, 100, 100)];
    view3.backgroundColor = [UIColor whiteColor];
    [view addSubview:view3];
}


效果图
请添加图片描述

和第一种情况是一样的

结论

添加灰色滤镜是图的时候,滤镜视图从滤镜被添加到的视图开始向上穿透,知道被有背景言色的视图挡住为止

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值