[ 夜间模式 ] 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总结了一个通用的模板,这个技术栈在前端开发者中是非常常见的. 总的来说 ...
随机推荐
- web报告工具FineReport在使用方法和解决方案常见错误遇到(一)
FineReport在使用方法和解决方案常见错误遇到(一) 这里写的开胃菜.我希望我们能理清自己的问题和解决办法干出来的,Mark一点点.有利于所有. 失败搜索出,如果有一个文件,看看你的度娘那里.看 ...
- 可以部署在广域网执行QQ高仿版 GG2014 (源代码)
距上次GG V3.7版本号(可在广域网部署执行的QQ高仿版 -- GG叽叽V3.7.优化视频聊天.控制很多其它相关细节)的公布.已经有50天了,这50天对于GG来说.是一个重大的飞跃. 由于这段时 ...
- 【C语言探索之旅】 第二部分第二课:进击的指针,C语言的王牌!
内容简介 1.课程大纲 2.第二部分第二课: 进击的指针,C语言的王牌 3.第二部分第三课预告: 数组 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言 ...
- java代码 分解EXCEL(一)
一,service层接口定义: ExcelParseService.java 一,service层接口实现: ExcelParseServiceImpl.java watermark/2/text/a ...
- 错误: 无法找到或可以不被加载到主类 Main
于eclipse导入Javaproject,执行错误:错误: 无法找到或可以不被加载到主类 Main! 百思不得其解,该解决方案是非常在线,但不是正确的方式,最后,例如,由下列溶液: 打开debug ...
- 【PHP】PHP获得第一章
一,PHP上部和下部壳体敏感 1)所有的用户定义的函数.类和keyword敏感. 例如以下结果输出一致: echo "hello world" Echo "hello ...
- mbed列--基于飞思卡尔FRDM KL25Z鼠标设计的高速实现
========================================================== 原创文章转载请注明:blog.csdn.net/guo8113 ========= ...
- S性能 Sigmoid Function or Logistic Function
S性能 Sigmoid Function or Logistic Function octave码 x = -10:0.1:10; y = zeros(length(x), 1); for i = 1 ...
- GoldenGate组态(四)它veridata组态
GoldenGate组态(四)它veridata组态 环境: Item Source System Target System Platform Red Hat Enterprise Linux Se ...
- POJ 2329 (暴力+搜索bfs)
Nearest number - 2 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3943 Accepted: 1210 De ...