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];
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章