iOS App 页面置灰部分原理记录
一、资源置灰
1、App内置相关资源和主体图片,根据相关时间后台下发指令来触发相关主题生效时间
2、App采用线上资源包、皮肤,动态下发到端上
二、控件置灰
给主要控件置灰,比如UIImage/UIColor/UIWebView等
1、遍历视图,依次对相关控件处理
2、采用hook方式,对相关控件hook特殊处理
三、App整体添加滤镜
1、UIView.layer上 添加灰色滤镜
// 最顶层视图,承载滤镜,自身不接收、不拦截任何触摸事件
@interface UIViewOverLay : UIView
@end
@implementation UIViewOverLay
-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
return nil;
}
@end
UIWindow *window = App的Window;
UIViewOverLay *overlay = [[UIViewOverLay alloc]initWithFrame:self.window.bounds];
overlay.translatesAutoresizingMaskIntoConstraints = false;
overlay.backgroundColor = [UIColor lightGrayColor];
overlay.layer.compositingFilter = @"saturationBlendMode";
[window addSubview:overlay];
最后通过各种方法,要保证overlay在最顶层.
2、私有API CAFilter添加滤镜
//获取RGBA颜色数值
CGFloat r,g,b,a;
[[UIColor lightGrayColor] 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"];
//设置给window
window.layer.filters = [NSArray arrayWithObject:filter];
四、效果展示
借助IMYAppGrayStyle Pod库,简单实现置灰效果
1、全局置灰
实现:AppDelegate 初始化处调用,IMYAppGrayStyle.open()
2、局部置灰
实现:StudyManagerController 初始化地方,IMYAppGrayStyle.add(to: self.view)
参考: