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. Visual Studio 使用及调试必知必会

    原文:Visual Studio 使用及调试必知必会   一:C# CODING 技巧 1:TODO 然后 CTRL + W + T,打开任务列表,选中 Comments,就会显示所有待做的任务 2: ...

  2. C++指针和引用简介

    摘要 本文介绍C++指针和概念引用 这是一个指针 指针的类型 指针所指向的类型 指针表达式 指针与函数 什么是引用 指针引用差别 指针和引用的同样点和不同点 **什么是指针** 指针就是一个存放地址的 ...

  3. iOS如何添加照片模拟器(附带诉讼)

    刚開始做图片选择时,使用了最笨的办法给iphone模拟器添加照片. 方法一:首先打开safari.然后找到图片.点击图片,保存到本地(iphone): 方法二:拖动本地计算机的随意一张照片到iphon ...

  4. Java线

    线程是一个单一的程序流程.多线程是指一个程序,可以在同一时间执行多个任务.每个任务是由一个单独的线程以完成.那.够同一时候在一个程序中执行,而且每个线程完毕不同的任务.程序能够通过控制线程来控制程序的 ...

  5. jsp、Servlet相关知识介绍(转)

    1.servlet生命周期 所谓生命周期,指的是servlet容器如何创建servlet实例.分配其资源.调用其方法.并销毁其实例的整个过程. 阶段一: 实例化(就是创建servlet对象,调用构造器 ...

  6. Android虚拟机器学习总结Dalvik虚拟机创建进程和线程分析

    Dalvik调用一个成员函数时,虚拟机,假设发现,该成员函数是一个JNI办法,然后,它会直接跳转到其地址来运行.也就是说.JNI方法是直接在本地操作系统上运行的.而不是由Dalvik虚拟机解释器运行. ...

  7. JNDI-j2ee

    Database Connection Pool (DBCP) Configurations The default database connection pool implementation i ...

  8. Qt Mac 在软件 icns图标制作

    1.首先,下载一个电话Icon Composer软件 之前Xcode像这个东西,现在,我不知道有或无,迷茫,一世Xcode很少. Icon Composer是苹果出的. 下载地址: http://ww ...

  9. 提高duilib的richedit控制的一些特征

    转载请注明原始出处.谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/41208207 假设要使用透明异形窗口功能,首先要改进duilib库让他 ...

  10. hdu 4915 Parenthese sequence 多校第五场

    推断一个序列是否是有效是简单的. 可是推断序列是不是有多个解会出问题. 那么从i=0 ~l 假设读到问号,推断该问号成为(能否有效,该问号为)是否有效. 假设都有效,则必有多个解. 假设都无效,则无解 ...