您的位置:首页 > 移动开发 > IOS开发

今天给大家分享ios中的夜间模式(通知中心)

2015-02-21 22:47 549 查看
这个夜间模式不是颜色反转的那种,其实很简单,就是在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];

    

    

    // Override point for customization after application launch.

    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];
    }

很简单吧,两个通知中心控制这层view的隐藏与显示,既简单又实用,需要的同学收藏一下吧!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios夜间模式