今天给大家分享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的隐藏与显示,既简单又实用,需要的同学收藏一下吧!
这段代码要在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中的夜间模式(通知中心)
- UIday2202:IOS设计模式:通知中心
- iOS监听模式系列之通知中心
- IOS观察者模式与通知中心
- 通知中心(以夜间模式为例)
- IOS通知中心(观察者模式)[NSNotificationCenter defaultCenter]
- 今天给大家分享ios中下拉图片变大效果
- iOS监听模式系列之通知中心
- iOS开发中单例设计模式、通知中心的使用
- iOS 设计模式-NSNotificationCenter 通知中心
- IOS观察者模式与通知中心
- 今天写代码是遇到的趣事,写出来与大家分享下!
- 【读书笔记】建造者模式代码完成与大家分享
- 从iOS开发者大会pdf看到的一些东西,跟大家分享
- 今天写了一个webservice的loadrunner脚步和大家分享一下
- struts2错误信息的几种显示 今天总结一下 分享给大家
- 迫不及待的和大家分享iOS下的正则表达式类库的心得!
- InstallShield 2011 今天正式发布啦,先把新功能和亮点与大家分享下!
- 今天有些失落,给大家分享一篇文章,希望咱们都能以此明心智。
- 今天在写程序中遇到的问题与大家分享一下