ios中的夜间模式(通知中心)

这个夜间模式不是颜色反转的那种,其实很简单,就是在window上面铺了一层view,把这个view的变成黑色,在调一下透明度就ok了,下面给大家看看代码实现吧
  • 这段代码要在AppDelegate中实现:
- (instancetype)init
{
    self = [super init];
    if (self) {

        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(receiveNotifica:) name:@"heiyejianglin" object:nil];
        // 白天模式
        NSNotificationCenter *center2 = [NSNotificationCenter defaultCenter];
        [center2 addObserver:self selector:@selector(receiveNotifica2:) name:@"limingjianglin" object:nil];

    }
    return self;
}
- (void)receiveNotifica:(NSNotification *)center
{
    self.nigView.hidden = NO;
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value",@"key", nil];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"limingjianglinla" object:@"香皂" userInfo: dic];
}
- (void)receiveNotifica2:(NSNotification *)center
{
    self.nigView.hidden = YES;
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value",@"key", nil];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"heiyejianglinla" object:@"香皂" userInfo: dic];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.nigView = [[UIView alloc] initWithFrame:self.window.bounds];
    self.nigView.userInteractionEnabled = NO;
    self.nigView.hidden = YES;
    self.nigView.backgroundColor = [UIColor blackColor];
    self.nigView.alpha = 0.6;
    [self.window addSubview:self.nigView];
    [_nigView release];

    return YES;
}

- 这段代码要在你加夜间模式中的开关方法中实现

if (button) {
        // NSLog(@"是");

        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value",@"key", nil];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"heiyejianglin" object:@"香皂" userInfo: dic];
    }else {
        // NSLog(@"否");

        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value",@"key", nil];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"limingjianglin" object:@"香皂" userInfo: dic];
    }

你可能感兴趣的:(黑夜模式)