美文网首页
iOS - App全局灰色调

iOS - App全局灰色调

作者: 缘來諟夢 | 来源:发表于2022-12-05 16:12 被阅读0次

    iOS - App全局灰色调

    在一些节日或者哀悼日,需要App全局变灰(或部分页面变灰色),此方法iOS13及以上高版本有效。
    原理就是把App页面当成图像,另一副偏灰图像和这个图像进行叠加运算,从而得到新的图像。iOS提供了Core Image滤镜,这些滤镜可以设置在UIView.layer上。将滤镜放置到App的最顶层。

    //添加到最顶层视图,承载滤镜,自身不接收、不拦截任何触摸事件
    @interface UIViewOverLay : UIView
    
    @end
    
    @implementation UIViewOverLay
    
    -(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event{
      return nil;
    }
    
    @end
    
    //添加到根视图,全局变灰
    UIViewOverLay* overLay = [[UIViewOverLay alloc] initWithFrame:self.window.bounds];
    overLay.translatesAutoresizingMaskIntoConstraints = NO;
    overLay.backgroundColor = [UIColor lightGrayColor];
    overLay.layer.compositingFilter = @"saturationBlendMode";
    overLay.layer.zPosition = FLT_MAX;
    //要保证overLay在最顶层
    [self.window addSubview:overLay];
    
    //如果要部分页面变灰,就添加到哪个具体页面
    
    

    </article>

    4人点赞

    iOS随笔

    相关文章

      网友评论

          本文标题:iOS - App全局灰色调

          本文链接:https://www.haomeiwen.com/subject/eupvfdtx.html