iOS开发

iOS下适配黑白模式

2023-06-28  本文已影响0人  万年老参

公祭日等场合,希望app指定页面设置为黑白模式以表达哀悼肃穆
方法1:使用“CAFilter”,可以给需要的layer设置为黑白模式。
风险:“CAFilter”是私有类,有审核风险

[AppDelegate getAppDelegate].window.layer.filters = @[({
    CIFilter *filter = [NSClassFromString(@"CAFilter") filterWithName:@"colorSaturate"];  // 看是 CIFilter,实际上是 CAFilter
    [filter setValue:@1 forKey:@"inputAmount"];
    filter;
})];

方法2:增加黑白遮罩层。
风险:iOS12以上可用,增加view层,改动了原项目视图层级。

//定义一个不接收点击事件的页面:
@interface ZRLandlordHPGrayView : UIView

@end

@implementation ZRLandlordHPGrayView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    return nil;
}

在需要黑白模式的的页面添加以下代码
并在需要开启黑白模式时调用此方法

- (void)showGrayViewWithSuperView:(UIView *)superView
{

        if (@available(iOS 12.0, *)) {//只支持12及以上
            ZRLandlordHPGrayView *overlay = [[ZRLandlordHPGrayView alloc] initWithFrame:superView.bounds];
            overlay.userInteractionEnabled = NO;
            overlay.translatesAutoresizingMaskIntoConstraints = false;
            overlay.backgroundColor = [UIColor grayColor];
            overlay.layer.compositingFilter = @"saturationBlendMode";
            [superView addSubview:overlay];
            [superView bringSubviewToFront:overlay];
//        }
    }
}

iOS中的H5页面:

通过JS注入+runtime入侵的方式,
风险:使用了runtime方法交换,如果项目中也有别的WKWebView分类,需要注意加载顺序。
页面生成之前要设置好是否开启黑白模式,无法在页面生成后修改。

WKWebView+blackWhiteModel.m文件:

#import "WKWebView+blackWhiteModel.h"
#import <objc/runtime.h>

@implementation WKWebView (blackWhiteModel)
+ (void)load {
    Method customMethod = class_getInstanceMethod([self class], @selector(gl_initWithFrame:configuration:));
    Method originMethod = class_getInstanceMethod([self class], @selector(initWithFrame:configuration:));
    method_exchangeImplementations(customMethod, originMethod);//方法交换
}


- (instancetype)gl_initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration
{
//全局定义的条件:是否开启黑白模式
    BOOL isOpenWhiteBlackModel = [[NSUserDefaults standardUserDefaults] boolForKey:@"kIsShowBlackWhiteModel"];
    if (isOpenWhiteBlackModel) {
        // js脚本
        NSString *jScript = @"var filter = '-webkit-filter:grayscale(100%);-moz-filter:grayscale(100%); -ms-filter:grayscale(100%); -o-filter:grayscale(100%) filter:grayscale(100%);';document.getElementsByTagName('html')[0].style.filter = 'grayscale(100%)';";
        // 注入
        WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
                     
        WKUserContentController *wkUController = [[WKUserContentController alloc] init];
           [wkUController addUserScript:wkUScript];
        // 配置对象
        WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
        wkWebConfig.userContentController = wkUController;
        configuration = wkWebConfig;
        WKWebView *webView = [self gl_initWithFrame:frame configuration:configuration];
        return webView;
    }
    
    return [self gl_initWithFrame:frame configuration:configuration];
}
@end
上一篇 下一篇

猜你喜欢

热点阅读