UIKit

iOS App页面置灰

2022-12-05  本文已影响0人  丶天空蓝丶

由于某些原因,需要将App全部页面或者某些页面置灰一段时间。下面就介绍下可以实现的几种方法。

方案一:

给App添加一层灰色滤镜,将App所有的视图通过滤镜,都变为灰色,也就是在window或者界面的view上添加这样一种灰色滤镜效果,使得整个App或者对应的界面变为灰色。

//创建一个View,当作滤镜使用
@interface UIViewOverLay : UIView
@end

@implementation UIViewOverLay
// .m 里面这个方法是必须的,表示该View不接受、不拦截任何触摸事件
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    return nil;
}

@end

在需要用到的地方使用

     UIViewOverLay *overlay = [[UIViewOverLay alloc] initWithFrame:self.view.bounds];
    overlay.translatesAutoresizingMaskIntoConstraints = false;
//    滤镜的背景颜色,一些鲜艳的颜色设置后会无效
    overlay.backgroundColor = [UIColor lightGrayColor];
/*    compositingFilter的值有如下: "normalBlendMode","darkenBlendMode","multiplyBlendMode","colorBurnBlendMode",
      "lightenBlendMode","screenBlendMode","colorDodgeBlendMode","overlayBlendMode","softLightBlendMode",
      "hardLightBlendMode","differenceBlendMode","exclusionBlendMode","hueBlendMode","saturationBlendMode",
    "colorBlendMode","luminosityBlendMode",     */
    overlay.layer.compositingFilter = @"saturationBlendMode";
//    设置图层在最上面
    overlay.layer.zPosition = FLT_MAX;
    [self.view addSubview:overlay];
//    如果在整个app上起作用,将这个view添加到window上即可
//    UIWindow *window = [[UIApplication sharedApplication].delegate window];
//    [window addSubview:overlay];

\color{red}{注意:} 这个方法只在iOS13及以上系统有效,iOS12部分有效部分无效。无效会导致整个节目变成全灰,谨慎使用。

方案二:

可以通过CAFilter这个私有类,设置一个滤镜,先将要显示的视图转为会单色调(即黑白色),然后再将整个视图的背景颜色设置为灰色,来达到置灰效果

    //获取RGBA颜色数值
    CGFloat r,g,b,a;
    [[UIColor whiteColor] getRed:&r green:&g blue:&b alpha:&a];
    //创建滤镜
    id cls = NSClassFromString(@"CAFilter");
    id filter = [cls filterWithName:@"colorMonochrome"];
    //设置滤镜参数
    [filter setValue:@[@(r),@(g),@(b),@(a)] forKey:@"inputColor"];
    [filter setValue:@(0) forKey:@"inputBias"];
    [filter setValue:@(1) forKey:@"inputAmount"];
    self.view.layer.filters = [NSArray arrayWithObject:filter];
//    UIWindow *window = [[UIApplication sharedApplication].delegate window];
//    window.layer.filters = [NSArray arrayWithObject:filter];

该方法优点是不受系统限制,但缺点就是展示效果不像第一种通过饱和度来调整的自然,感觉像真的盖了一层灰色的蒙层到App上,而且因为使用的私用类CAFilter,具有风险性

方案三:

界面置灰主要将界面上面的图片和颜色改成灰色就好了

//图片置灰
-(UIImage *)getGrayImage:(UIImage *)sourceImage {
    int width = sourceImage.size.width;
    int height = sourceImage.size.height;
    
    CGColorSpaceRef colorRef = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate(nil, width, height, 8, 0, colorRef, kCGImageAlphaNone);
    CGColorSpaceRelease(colorRef);
    
    if (context == NULL) {
        return nil;
    }
    
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), sourceImage.CGImage);
    CGImageRef grayImageRef = CGBitmapContextCreateImage(context);
    UIImage *grayImage = [UIImage imageWithCGImage:grayImageRef];
    CGContextRelease(context);
    CGImageRelease(grayImageRef);
    
    return grayImage;
}
-(UIColor *)getGrayColor:(UIColor *)sourceColor {
    CGFloat r,g,b,a;
    [sourceColor getRed:&r green:&g blue:&b alpha:&a];
    CGFloat y = 0.299*r + 0.587*g + 0.114*b;
    UIColor *gray = [UIColor colorWithRed:y green:y blue:y alpha:a];
    return gray;
}

优点:不存在潜在问题
缺点:图片因为是重新绘制的,清晰度会变模糊。处理本地图片和颜色外,如:网络图片、layer、webview等都没有考虑进去,不太全面。开发阶段需要考虑好,后续如果整体替换还可以通过方法替换,如果只有部分界面改变则要针对里面图片和颜色一个个修改。

附带小程序置灰方法:在page里面添加: filter: grayscale(100%);

上一篇下一篇

猜你喜欢

热点阅读