#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface IMYAppGrayStyle : NSObject
/// 開(kāi)啟全局變灰
+ (void)open;
/// 關(guān)閉全局變灰
+ (void)close;
/// 添加灰色模式
+ (void)addToView:(UIView *)view;
/// 移除灰色模式
+ (void)removeFromView:(UIView *)view;
@end
NS_ASSUME_NONNULL_END
#import "IMYAppGrayStyle.h"
@interface IMYAppGrayStyleCoverView : UIView
@end
@implementation IMYAppGrayStyleCoverView
+ (NSHashTable *)allCoverViews {
? ? static NSHashTable *array;
? ? static dispatch_once_t onceToken;
? ? dispatch_once(&onceToken, ^{
? ? ? ? array = [NSHashTable weakObjectsHashTable];
? ? });
? ? return array;
}
+ (void)showInMaskerView:(UIView *)maskerView {
? ? if (!@available(iOS 13, *)) {
? ? ? ? // iOS13 之前系統(tǒng)不支持
? ? ? ? return;
? ? }
? ? // 遍歷是否已添加 gray cover view
? ? for (UIView *subview in maskerView.subviews) {
? ? ? ? if ([subview isKindOfClass:IMYAppGrayStyleCoverView.class]) {
? ? ? ? ? ? return;
? ? ? ? }
? ? }
? ? IMYAppGrayStyleCoverView *coverView = [[self alloc] initWithFrame:maskerView.bounds];
? ? coverView.userInteractionEnabled = NO;
? ? coverView.backgroundColor = [UIColor lightGrayColor];
? ? coverView.layer.compositingFilter = @"saturationBlendMode";
? ? coverView.layer.zPosition = FLT_MAX;
? ? [maskerView addSubview:coverView];
? ? [self.allCoverViews addObject:coverView];
}
@end
@implementation IMYAppGrayStyle
+ (void)open {
? ? NSAssert(NSThread.isMainThread, @"必須在主線程調(diào)用!");
? ? NSMutableSet *windows = [NSMutableSet set];
? ? [windows addObjectsFromArray:UIApplication.sharedApplication.windows];
? ? if (@available(iOS 13, *)) {
? ? ? ? for (UIWindowScene *scene in UIApplication.sharedApplication.connectedScenes) {
? ? ? ? ? ? if (![scene isKindOfClass:UIWindowScene.class]) {
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? [windows addObjectsFromArray:scene.windows];
? ? ? ? }
? ? }
? ? // 遍歷所有window轧简,給它們加上蒙版
? ? for (UIWindow *window in windows) {
? ? ? ? NSString *className = NSStringFromClass(window.class);
? ? ? ? if (![className containsString:@"UIText"]) {
? ? ? ? ? ? [IMYAppGrayStyleCoverView showInMaskerView:window];
? ? ? ? }
? ? }
}
+ (void)close {
? ? NSAssert(NSThread.isMainThread, @"必須在主線程調(diào)用!");
? ? for (UIView *coverView in IMYAppGrayStyleCoverView.allCoverViews) {
? ? ? ? [coverView removeFromSuperview];
? ? }
}
+ (void)addToView:(UIView *)view {
? ? [IMYAppGrayStyleCoverView showInMaskerView:view];
}
+ (void)removeFromView:(UIView *)view {
? ? for (UIView *subview in view.subviews) {
? ? ? ? if ([subview isKindOfClass:IMYAppGrayStyleCoverView.class]) {
? ? ? ? ? ? [subview removeFromSuperview];
? ? ? ? }
? ? }
}
@end