DKNightVersion框架、重写管理类 & 控件的分类!--可重写
{ 使用GCD、runtime、delegate等 & 工具类的创建 }

================

1、管理类的头文件 NightVersionManager.h

 

定义宏,通过RGB获取颜色!

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

定义头文件.h

typedef enum : NSUInteger {
   DKThemeVersionNormal,
   DKThemeVersionNight,
} DKThemeVersion;

extern NSString *const DKNightVersionNightFallingNotification;
extern NSString *const DKNightVersionDawnComingNotification;

extern CGFloat const DKNightVersionAnimationDuration;

@interface DKNightVersionManager : NSObject
+ (DKThemeVersion)currentThemeVersion;
+ (void)nightFalling;
+ (void)dawnComing;
+ (BOOL)useDefaultNightColor;
+ (void)setUseDefaultNightColor:(BOOL)use;

2、核心代码 NightVersionManager.m

2.1 单例、保证工具类对象,只被分配一次内存

+ (DKNightVersionManager *)sharedNightVersionManager {
   static dispatch_once_t once;
   static DKNightVersionManager *instance;
   dispatch_once(&once, ^{
       instance = [self new];
       instance.useDefaultNightColor = YES;
   });
   return instance;
}

2.2 设置主题的版本

- (void)setThemeVersion:(DKThemeVersion)themeVersion {
   if (_themeVersion == themeVersion) {
       // if type does not change, don't execute code below to enhance performance.
       return;
   }
   _themeVersion = themeVersion;
   [self changeColor:[[UIApplication sharedApplication].delegate.window.subviews firstObject]];
}

2.3 改变颜色--委托

- (void)changeColor:(id <DKNightVersionSwichColorProtocol>)object {
   if ([object respondsToSelector:@selector(changeColor)]) {
       [object changeColor];
   }
   if ([object respondsToSelector:@selector(subviews)]) {
       if (![object subviews]) {
           // Basic case, do nothing.
           return;
       } else {
           for (id subview in [object subviews]) {
               // recursice darken all the subviews of current view.
               [self changeColor:subview];
               if ([subview respondsToSelector:@selector(changeColor)]) {
                   [subview changeColor];
               }
           }
       }
   }
}

2.4 设置模式的颜色

+ (BOOL)useDefaultNightColor {
   return self.sharedNightVersionManager.useDefaultNightColor;
}

+ (void)setUseDefaultNightColor:(BOOL)use {
   [self.sharedNightVersionManager setUseDefaultNightColor:use];
}

3、控件分类(UIButton、UILabel、UIScrollView等)

3.1 UIButton+NightVersion.m

- (void)changeColor {

   [UIView animateWithDuration:DKNightVersionAnimationDuration animations:^{

       [self setTitleColor:([DKNightVersionManager currentThemeVersion] == DKThemeVersionNight) ? self.nightTitleColor : self.normalTitleColor forState:UIControlStateNormal];

       [self setBackgroundColor:([DKNightVersionManager currentThemeVersion] == DKThemeVersionNight) ? self.nightBackgroundColor : self.normalBackgroundColor];

       [self setTintColor:([DKNightVersionManager currentThemeVersion] == DKThemeVersionNight) ? self.nightTintColor : self.normalTintColor];

   }];
}

3.2 UIButton+TitleColor.m

> 加载时GCD,保证线程安全。

> runtime运行时,SEL & Method的使用。

+ (void)load {
   static dispatch_once_t onceToken;
   dispatch_once(&onceToken, ^{
       Class class = [self class];
       SEL originalSelector = @selector(setTitleColor:forState:);
       SEL swizzledSelector = @selector(hook_setTitleColor:forState:);
       Method originalMethod = class_getInstanceMethod(class, originalSelector);
       Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
       BOOL didAddMethod =
       class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
       if (didAddMethod){
           class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
       } else {
           method_exchangeImplementations(originalMethod, swizzledMethod);
       }
   });
}

设置默认的标题颜色

- (UIColor *)defaultNightTitleColor {
   if ([self isMemberOfClass:[UIButton class]]) {
       return UIColorFromRGB(0x5F80AC);
   } else {
       UIColor *resultColor = self.normalTitleColor ?: [UIColor clearColor];
       return resultColor;
   }
}

4、Test测试

4.1 AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   // Override point for customization after application launch.
   self.window.backgroundColor = [UIColor whiteColor];
   [self.window makeKeyAndVisible];
   UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]];
   self.window.rootViewController = navigation;
   return YES;
}

4.2 RootViewController.m

- (void)nightFalls {
   [DKNightVersionManager nightFalling];
}

- (void)dawnComes {
   [DKNightVersionManager dawnComing];
}

- (void)push {
   [self.navigationController pushViewController:[[SuccViewController alloc] init] animated:YES];
}

4.3 SuccViewController.m

- (void)viewDidLoad {
   [super viewDidLoad];
   self.view.backgroundColor = [UIColor whiteColor];
   self.view.nightBackgroundColor = [UIColor colorWithRed:0.141 green:0.145 blue:0.153 alpha:1.0];
}

================

PS:

[ 每日一句 

" Smiling is the best reaction in all situations. "

开源框架

http://www.umeng.com/

================

|--> Copyright (c) 2015 Bing Ma.

|--> GitHub RUL: https://github.com/SpongeBob-GitHub

[ 夜间模式 ] NightVersion的更多相关文章

  1. android夜间模式实现

    一.概述 android夜间模式实现分为两大类 重启activity的实现 不重启activity的实现 二.正文 1.重启activity实现夜间模式[在界面文件中的实现部分] 1.1在attrs. ...

  2. DKNightVersion 的实现 --- 如何为 iOS 应用添加夜间模式

    在很多重阅读或者需要在夜间观看的软件其实都会把夜间模式当做一个 App 所需要具备的特性. 而如何在不改变原有的架构, 甚至不改变原有的代码的基础上, 就能为应用优雅地添加夜间模式就成为一个在很多应用 ...

  3. WPF窗口阴影和夜间模式的实现

    窗口阴影 实现 因项目需要给用户一定提示,设计师建议在鼠标进入时显示窗口阴影,离开时取消窗口阴影. 很自然,都会想到直接在窗口的内容或者自定义窗口的最外层元素上加效果.示例如下: <Grid&g ...

  4. WP8版微信5.4发布 新增夜间模式 暂没小视频

    经过近一个月的内测,WP8版的微信终于更新了v 5.4版本.新增聊天中的照片墙.识别图片二维码.夜间模式等功能,还对资源占用情况进行了优化,让程序可以更流畅的在低配置设备上运行. 不过,WP8版微信5 ...

  5. android简单的夜间模式

    现在android项目values下打 attrs.xml <?xml version="1.0" encoding="utf-8"?> <r ...

  6. Android白天/夜间模式Day/Night Mode标准原生SDK实现

     Android白天/夜间模式Day/Night Mode标准原生SDK实现 章节A:Android实现白天/夜间模式主要控制器在于UiModeManager,UiModeManager是Andr ...

  7. Android 之夜间模式(多主题)的实现

    引言 夜间模式其实属于多主题切换的一种,不过是最麻烦的一种.因为在夜间模式下不仅要切换主色调,次要色调等等,还要覆盖一些特殊的颜色,因为在夜间模式下总不能什么都是黑的把,那不得丑死-.-,所以当你夜间 ...

  8. DKNightVersion的基本使用(夜间模式)

    DKNightVersion下载地址: https://github.com/Draveness/DKNightVersion 基本原理就是利用一个单例对象来存储颜色, 然后通过runtime中的ob ...

  9. ReactJS React+Redux+Router+antDesign通用高效率开发模板,夜间模式为例

    工作比较忙,一直没有时间总结下最近学习的一些东西,为了方便前端开发,我使用React+Redux+Router+antDesign总结了一个通用的模板,这个技术栈在前端开发者中是非常常见的. 总的来说 ...

随机推荐

  1. 对于Netty的十一个疑问(转)

    [说明]本文原载于码农 IO(manong.io)官方微信 developerWorks,转载.引用请注明出处及作者. 1.Netty 是什么? Netty 是一个基于 JAVA NIO 类库的异步通 ...

  2. activity-alias使用

    activity-alias这是android为了重新使用Activity设计. 当Activity的onCreate()在方法,运行getIntent().getComponent().getCla ...

  3. Centos6.5下一个Ceph存储集群结构

    简单的介绍 Ceph的部署模式下主要包括下面几个类型的节点 • Ceph OSDs: A Ceph OSD 进程主要用来存储数据,处理数据的replication,恢复,填充.调整资源组合以及通过检查 ...

  4. 在Ceph创建虚拟机的过程改进分析

    作为个人学习笔记分享.有不论什么问题欢迎交流! 近期在Gerrit中看到一个change:https://review.openstack.org/#/c/94295/ , 它主要是对当前在Ceph中 ...

  5. 《web全栈工程师的自我修养》阅读笔记

    在买之前以为这本书是教你怎么去做一个web全栈工程师,以及介绍需要掌握的哪些技术的书,然而看的过程中才发现,是一本方法论的书.读起来的感觉有点像红衣教主的<我的互联网方法论>,以一些自己的 ...

  6. Extjs GridPanel 几点说明

    1. 在Ext中,表格控件必须包括列定义信息,并指定表格的数据存储器.列信息由columns定义,而数据存储器有store定义. 2. store负责把各种各样的原始数据(JSON对象数组等等)转换成 ...

  7. Html5响应式设计与实现广场

    由于提出的想法响应式设计,越来越多的网站使用这样的思想.各类大型网站如雨后春笋般涌了出来.例如:小米商城.天猫等. 至于响应式设计的概念等大家能够去百度百度,我这里就不相信解说了.直接为大家带来源代码 ...

  8. Nginx搭建反向代理服务器过程详解(转)

    一.反向代理 我们都知道,80端口是web服务的默认端口,其他主机访问web服务器也是默认和80端口进行web交互,而一台服务器也只有一个80端口,这是约定俗成的标准. 我们来看下面两个场景: 1.服 ...

  9. 当向后台插入或读取JSON数据遇见回车时

    今天在项目中发现.当插入或读取JSON数据时遇见回车符.返回JSON数据格式时会报错(firebug里体现为乱码),百度了一下发现JSON不支持字符串里存在回车! 解决的方法: 在向接口插入带json ...

  10. Eclipse SDK构建J2EE开发环境

    鄙视官Java EE Developers 体积庞大的兄弟们可以提出自己的J2EE开发环境! 1.第一次去Eclipse官网下载Eclipse IDE 我使用的是:Eclipse IDE for Ja ...