zoukankan      html  css  js  c++  java
  • [ 夜间模式 ] 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

     



    " There's always more to learn, and there are always better ways to do what you've done before. " -- Trybst


  • 相关阅读:
    [转载]Quartus ii 一些Warning/Eeror分析与解决
    [转载]MAC、IP头、TCP头、UDP头帧格式、详解
    [转载]关于晶振ppm
    20140919-FPGA-有效观察设计中的差分信号
    [转载]转一篇Systemverilog的一个牛人总结
    [转载]ISE中COE与MIF文件的联系与区别
    linux系统性能监控--I/O利用率
    linux系统性能监控--内存利用率
    linux系统性能监控--CPU利用率
    在linux系统中I/O 调度的选择
  • 原文地址:https://www.cnblogs.com/Trybst/p/4495709.html
Copyright © 2011-2022 走看看