[ 夜间模式 ] NightVersion
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的更多相关文章
- android夜间模式实现
一.概述 android夜间模式实现分为两大类 重启activity的实现 不重启activity的实现 二.正文 1.重启activity实现夜间模式[在界面文件中的实现部分] 1.1在attrs. ...
- DKNightVersion 的实现 --- 如何为 iOS 应用添加夜间模式
在很多重阅读或者需要在夜间观看的软件其实都会把夜间模式当做一个 App 所需要具备的特性. 而如何在不改变原有的架构, 甚至不改变原有的代码的基础上, 就能为应用优雅地添加夜间模式就成为一个在很多应用 ...
- WPF窗口阴影和夜间模式的实现
窗口阴影 实现 因项目需要给用户一定提示,设计师建议在鼠标进入时显示窗口阴影,离开时取消窗口阴影. 很自然,都会想到直接在窗口的内容或者自定义窗口的最外层元素上加效果.示例如下: <Grid&g ...
- WP8版微信5.4发布 新增夜间模式 暂没小视频
经过近一个月的内测,WP8版的微信终于更新了v 5.4版本.新增聊天中的照片墙.识别图片二维码.夜间模式等功能,还对资源占用情况进行了优化,让程序可以更流畅的在低配置设备上运行. 不过,WP8版微信5 ...
- android简单的夜间模式
现在android项目values下打 attrs.xml <?xml version="1.0" encoding="utf-8"?> <r ...
- Android白天/夜间模式Day/Night Mode标准原生SDK实现
Android白天/夜间模式Day/Night Mode标准原生SDK实现 章节A:Android实现白天/夜间模式主要控制器在于UiModeManager,UiModeManager是Andr ...
- Android 之夜间模式(多主题)的实现
引言 夜间模式其实属于多主题切换的一种,不过是最麻烦的一种.因为在夜间模式下不仅要切换主色调,次要色调等等,还要覆盖一些特殊的颜色,因为在夜间模式下总不能什么都是黑的把,那不得丑死-.-,所以当你夜间 ...
- DKNightVersion的基本使用(夜间模式)
DKNightVersion下载地址: https://github.com/Draveness/DKNightVersion 基本原理就是利用一个单例对象来存储颜色, 然后通过runtime中的ob ...
- ReactJS React+Redux+Router+antDesign通用高效率开发模板,夜间模式为例
工作比较忙,一直没有时间总结下最近学习的一些东西,为了方便前端开发,我使用React+Redux+Router+antDesign总结了一个通用的模板,这个技术栈在前端开发者中是非常常见的. 总的来说 ...
随机推荐
- Alamofire网络库基础教程
原文 Beginning Alamofire Tutorial 原文作者 Essan Parto译者 星夜暮晨(QQ:412027805) http://www.jianshu.com/p/f1208 ...
- Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock’
今天服务器遇到了一个很熟悉的问题 输入 #mysql -u root -p ERROR 2002 (HY000): Can't connect to local MySQL server throug ...
- DisplayContent、StackBox、TaskStack笔记
文章仅零散记录自己的一点理解,仅供自己參考. 每一个显示设备,都有一个Display对象,DisplayManagerService专门管理这些Display. 1.DisplayContent() ...
- POJ 1699 Best Sequence (DFS+预处理)
意甲冠军:看图片是晶莹剔透的,正确的, N连接到第一序列(同样的序列部分).总序列获得最短. 主题链接:http://poj.org/problem?id=1699 ~~~~ 思路就是:将N个序列首尾 ...
- dispatch_once认识分析
dispatch_once为了确保代码运行一次 +(NSDateFormatter*)getDBDateFormat { static NSDateFormatter* format; static ...
- UVA 239 - Tempus et mobilius. Time and motion(更换周期)
UVA 239 - Tempus et mobilius. Time and motion 题目链接 题意:这题题意也是吊得飞起,看了老半天,大概是这样: 有一个放球的队列.和3个轨道(说白了就是栈) ...
- 微信oauth获取用户的信息页面授权
參考链接(请在微信client中打开此链接体验) Scope为snsapi_base https://open.weixin.qq.com/connect/oauth2/authorize?appid ...
- SCM白色幼儿系列(十二) Proteus仿真软件简介
Proteus软件是英国Labcenter electronics公司出版的EDA工具软件.经常使用于单片机等数字电路仿真,分为ISIS和ARES两个程序,前者用于仿真,后者用于设计PCB.我们常使用 ...
- cocos2d-x3.0 windows 环境配置
cocos2d-x3.0 windows 环境配置 参考Oo泡泡糖oO的CSDN博文 :http://blog.csdn.net/u010296979/article/details/24273393 ...
- 【Java编码准则】の #02不要在client存储未加密的敏感信息
当构建CS模式的应用程序时,在client側存储敏感信息(比如用户私要信息)可能导致非授权的信息泄漏. 对于Web应用程序来说,最常见的泄漏问题是在client使用cookies存放server端获取 ...