diff --git a/Example/Example.xcodeproj/project.pbxproj b/Example/Example.xcodeproj/project.pbxproj index d2cf20ac..a90d9e79 100644 --- a/Example/Example.xcodeproj/project.pbxproj +++ b/Example/Example.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 37A0AF191EBDAC5700CE059B /* DKAttribute.m in Sources */ = {isa = PBXBuildFile; fileRef = 37A0AF181EBDAC5700CE059B /* DKAttribute.m */; }; + 37A0AF1C1EBDAC6300CE059B /* UITabBarItem+Night.m in Sources */ = {isa = PBXBuildFile; fileRef = 37A0AF1B1EBDAC6300CE059B /* UITabBarItem+Night.m */; }; 7261ADF91CB9FB2D007E283C /* PresentingViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7261ADF21CB9FB2D007E283C /* PresentingViewController.m */; }; 7261ADFA1CB9FB2D007E283C /* RootViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7261ADF41CB9FB2D007E283C /* RootViewController.m */; }; 7261ADFB1CB9FB2D007E283C /* SuccViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7261ADF61CB9FB2D007E283C /* SuccViewController.m */; }; @@ -22,6 +24,10 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 37A0AF171EBDAC5700CE059B /* DKAttribute.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DKAttribute.h; sourceTree = ""; }; + 37A0AF181EBDAC5700CE059B /* DKAttribute.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DKAttribute.m; sourceTree = ""; }; + 37A0AF1A1EBDAC6300CE059B /* UITabBarItem+Night.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UITabBarItem+Night.h"; sourceTree = ""; }; + 37A0AF1B1EBDAC6300CE059B /* UITabBarItem+Night.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UITabBarItem+Night.m"; sourceTree = ""; }; 7261ADF11CB9FB2D007E283C /* PresentingViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PresentingViewController.h; sourceTree = ""; }; 7261ADF21CB9FB2D007E283C /* PresentingViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PresentingViewController.m; sourceTree = ""; }; 7261ADF31CB9FB2D007E283C /* RootViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RootViewController.h; sourceTree = ""; }; @@ -92,6 +98,10 @@ 729324041CB9FAF1009E544E /* Assets.xcassets */, 729324061CB9FAF1009E544E /* LaunchScreen.storyboard */, 729324091CB9FAF1009E544E /* Info.plist */, + 37A0AF171EBDAC5700CE059B /* DKAttribute.h */, + 37A0AF181EBDAC5700CE059B /* DKAttribute.m */, + 37A0AF1A1EBDAC6300CE059B /* UITabBarItem+Night.h */, + 37A0AF1B1EBDAC6300CE059B /* UITabBarItem+Night.m */, 729323F81CB9FAF1009E544E /* Supporting Files */, ); path = Example; @@ -180,6 +190,8 @@ 7261ADFC1CB9FB2D007E283C /* TableViewCell.m in Sources */, 729324001CB9FAF1009E544E /* ViewController.m in Sources */, 729323FD1CB9FAF1009E544E /* AppDelegate.m in Sources */, + 37A0AF1C1EBDAC6300CE059B /* UITabBarItem+Night.m in Sources */, + 37A0AF191EBDAC5700CE059B /* DKAttribute.m in Sources */, 729323FA1CB9FAF1009E544E /* main.m in Sources */, 7261ADF91CB9FB2D007E283C /* PresentingViewController.m in Sources */, 7261ADFB1CB9FB2D007E283C /* SuccViewController.m in Sources */, diff --git a/Example/Example/AppDelegate.m b/Example/Example/AppDelegate.m index 534bd513..1af7e023 100644 --- a/Example/Example/AppDelegate.m +++ b/Example/Example/AppDelegate.m @@ -9,6 +9,9 @@ #import "AppDelegate.h" #import "RootViewController.h" #import "SuccViewController.h" +#import "ViewController.h" + +#import "UITabBarItem+Night.h" @interface AppDelegate () @@ -18,13 +21,30 @@ @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + + RootViewController *vc1 = [[RootViewController alloc] init]; + vc1.tabBarItem.title = @"Root"; + [vc1.tabBarItem dk_setImage:DKImagePickerWithNames(@"tabBar_association_normal", @"tabBar_profile_normal") forState:(UIControlStateNormal)]; + [vc1.tabBarItem dk_setImage:DKImagePickerWithNames(@"tabBar_association_selected", @"tabBar_profile_selected") forState:(UIControlStateSelected)]; + [vc1.tabBarItem dk_setTitleAttributePicker:DKAttributePickerWithAttributes(@{NSForegroundColorAttributeName: [UIColor blackColor]}, @{NSForegroundColorAttributeName: [UIColor orangeColor]}, @{NSForegroundColorAttributeName: [UIColor grayColor]}) forState:(UIControlStateNormal)]; + [vc1.tabBarItem dk_setTitleAttributePicker:DKAttributePickerWithAttributes(@{NSForegroundColorAttributeName: [UIColor redColor]}, @{NSForegroundColorAttributeName: [UIColor blueColor]}, @{NSForegroundColorAttributeName: [UIColor greenColor]}) forState:(UIControlStateSelected)]; + UINavigationController *navigation1 = [[UINavigationController alloc] initWithRootViewController:vc1]; + + ViewController *vc2 = [[ViewController alloc] init]; + vc2.tabBarItem.title = @"My"; + [vc2.tabBarItem dk_setImage:DKImagePickerWithNames(@"tabBar_profile_normal", @"tabBar_association_normal") forState:(UIControlStateNormal)]; + [vc2.tabBarItem dk_setImage:DKImagePickerWithNames(@"tabBar_profile_selected", @"tabBar_association_selected") forState:(UIControlStateSelected)]; + [vc2.tabBarItem dk_setTitleAttributePicker:DKAttributePickerWithAttributes(@{NSForegroundColorAttributeName: [UIColor blackColor]}, @{NSForegroundColorAttributeName: [UIColor orangeColor]}, @{NSForegroundColorAttributeName: [UIColor grayColor]}) forState:(UIControlStateNormal)]; + [vc2.tabBarItem dk_setTitleAttributePicker:DKAttributePickerWithAttributes(@{NSForegroundColorAttributeName: [UIColor redColor]}, @{NSForegroundColorAttributeName: [UIColor blueColor]}, @{NSForegroundColorAttributeName: [UIColor greenColor]}) forState:(UIControlStateSelected)]; + UINavigationController *navigation2 = [[UINavigationController alloc] initWithRootViewController:vc2]; + self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; - // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; + UITabBarController *tabController = [[UITabBarController alloc] init]; + tabController.viewControllers = @[navigation1, navigation2]; + self.window.rootViewController = tabController; [self.window makeKeyAndVisible]; - UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]]; - self.window.rootViewController = navigation; - UISearchBar *searchBar = [[UISearchBar alloc] init]; + return YES; } diff --git a/Example/Example/Assets.xcassets/AppIcon.appiconset/Contents.json b/Example/Example/Assets.xcassets/AppIcon.appiconset/Contents.json index 118c98f7..b8236c65 100644 --- a/Example/Example/Assets.xcassets/AppIcon.appiconset/Contents.json +++ b/Example/Example/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -1,5 +1,15 @@ { "images" : [ + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "3x" + }, { "idiom" : "iphone", "size" : "29x29", diff --git a/Example/Example/Assets.xcassets/tabBar_association_normal.imageset/Contents.json b/Example/Example/Assets.xcassets/tabBar_association_normal.imageset/Contents.json new file mode 100644 index 00000000..5f93eb47 --- /dev/null +++ b/Example/Example/Assets.xcassets/tabBar_association_normal.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "filename" : "tabBar_association_normal@2x.png", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/Example/Example/Assets.xcassets/tabBar_association_normal.imageset/tabBar_association_normal@2x.png b/Example/Example/Assets.xcassets/tabBar_association_normal.imageset/tabBar_association_normal@2x.png new file mode 100644 index 00000000..a4eda3af Binary files /dev/null and b/Example/Example/Assets.xcassets/tabBar_association_normal.imageset/tabBar_association_normal@2x.png differ diff --git a/Example/Example/Assets.xcassets/tabBar_association_selected.imageset/Contents.json b/Example/Example/Assets.xcassets/tabBar_association_selected.imageset/Contents.json new file mode 100644 index 00000000..e22cd2db --- /dev/null +++ b/Example/Example/Assets.xcassets/tabBar_association_selected.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "filename" : "tabBar_association_selected@2x.png", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/Example/Example/Assets.xcassets/tabBar_association_selected.imageset/tabBar_association_selected@2x.png b/Example/Example/Assets.xcassets/tabBar_association_selected.imageset/tabBar_association_selected@2x.png new file mode 100644 index 00000000..39be7dae Binary files /dev/null and b/Example/Example/Assets.xcassets/tabBar_association_selected.imageset/tabBar_association_selected@2x.png differ diff --git a/Example/Example/Assets.xcassets/tabBar_profile_normal.imageset/Contents.json b/Example/Example/Assets.xcassets/tabBar_profile_normal.imageset/Contents.json new file mode 100644 index 00000000..e24136a1 --- /dev/null +++ b/Example/Example/Assets.xcassets/tabBar_profile_normal.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "filename" : "tabBar_profile_normal@2x.png", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/Example/Example/Assets.xcassets/tabBar_profile_normal.imageset/tabBar_profile_normal@2x.png b/Example/Example/Assets.xcassets/tabBar_profile_normal.imageset/tabBar_profile_normal@2x.png new file mode 100644 index 00000000..41f4e386 Binary files /dev/null and b/Example/Example/Assets.xcassets/tabBar_profile_normal.imageset/tabBar_profile_normal@2x.png differ diff --git a/Example/Example/Assets.xcassets/tabBar_profile_selected.imageset/Contents.json b/Example/Example/Assets.xcassets/tabBar_profile_selected.imageset/Contents.json new file mode 100644 index 00000000..2271015f --- /dev/null +++ b/Example/Example/Assets.xcassets/tabBar_profile_selected.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "filename" : "tabBar_profile_selected@2x.png", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/Example/Example/Assets.xcassets/tabBar_profile_selected.imageset/tabBar_profile_selected@2x.png b/Example/Example/Assets.xcassets/tabBar_profile_selected.imageset/tabBar_profile_selected@2x.png new file mode 100644 index 00000000..f5121342 Binary files /dev/null and b/Example/Example/Assets.xcassets/tabBar_profile_selected.imageset/tabBar_profile_selected@2x.png differ diff --git a/Example/Example/DKAttribute.h b/Example/Example/DKAttribute.h new file mode 100644 index 00000000..53460b43 --- /dev/null +++ b/Example/Example/DKAttribute.h @@ -0,0 +1,19 @@ +// +// DKAttribute.h +// ESports +// +// Created by 时彬强 on 2017/5/5. +// Copyright © 2017年 QQLS. All rights reserved. +// + +#import + +typedef NSString DKThemeVersion; + +typedef NSDictionary *(^DKAttributePicker)(DKThemeVersion *themeVersion); + +DKAttributePicker DKAttributePickerWithAttributes(NSDictionary *normal, ...); + +@interface DKAttribute : NSObject + +@end diff --git a/Example/Example/DKAttribute.m b/Example/Example/DKAttribute.m new file mode 100644 index 00000000..105320ca --- /dev/null +++ b/Example/Example/DKAttribute.m @@ -0,0 +1,41 @@ +// +// DKAttribute.m +// ESports +// +// Created by 时彬强 on 2017/5/5. +// Copyright © 2017年 QQLS. All rights reserved. +// + +#import "DKAttribute.h" + +#import +//#import "DKNightVersionManager.h" +//#import "DKColorTable.h" + +@implementation DKAttribute + +DKAttributePicker DKAttributePickerWithAttributes(NSDictionary *normal, ...) { + + NSArray *themes = [DKColorTable sharedColorTable].themes; + NSMutableArray *attributes = [[NSMutableArray alloc] initWithCapacity:themes.count]; + [attributes addObject:normal]; + NSUInteger num_args = themes.count - 1; + va_list colors_list; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wvarargs" + va_start(colors_list, num_args); +#pragma clang diagnostic pop + + for (NSUInteger i = 0; i < num_args; i++) { + NSDictionary *attribute = va_arg(colors_list, NSDictionary *); + [attributes addObject:attribute]; + } + va_end(colors_list); + + return ^(DKThemeVersion *themeVersion) { + NSUInteger index = [themes indexOfObject:themeVersion]; + return attributes[index]; + }; +} + +@end diff --git a/Example/Example/UITabBarItem+Night.h b/Example/Example/UITabBarItem+Night.h new file mode 100644 index 00000000..a044c438 --- /dev/null +++ b/Example/Example/UITabBarItem+Night.h @@ -0,0 +1,20 @@ +// +// UITabBarItem+Night.h +// ESports +// +// Created by 时彬强 on 2017/5/5. +// Copyright © 2017年 QQLS. All rights reserved. +// + +#import + +#import "DKAttribute.h" +#import +//#import "NSObject+Night.h" + +@interface UITabBarItem (Night) + +- (void)dk_setTitleAttributePicker:(DKAttributePicker)picker forState:(UIControlState)state; +- (void)dk_setImage:(DKImagePicker)picker forState:(UIControlState)state; + +@end diff --git a/Example/Example/UITabBarItem+Night.m b/Example/Example/UITabBarItem+Night.m new file mode 100644 index 00000000..12c64e24 --- /dev/null +++ b/Example/Example/UITabBarItem+Night.m @@ -0,0 +1,91 @@ +// +// UITabBarItem+Night.m +// ESports +// +// Created by 时彬强 on 2017/5/5. +// Copyright © 2017年 QQLS. All rights reserved. +// + +#import "UITabBarItem+Night.h" + +@interface UITabBarItem () + +@property (nonatomic, strong) NSMutableDictionary *pickers; + +@end + +@implementation UITabBarItem (Night) + +- (void)dk_setTitleAttributePicker:(DKAttributePicker)picker forState:(UIControlState)state { + [self setTitleTextAttributes:picker(self.dk_manager.themeVersion) forState:state]; + + NSString *key = [NSString stringWithFormat:@"%@", @(state)]; + NSMutableDictionary *dictionary = [self.pickers valueForKey:key]; + if (!dictionary) { + dictionary = [[NSMutableDictionary alloc] init]; + } + [dictionary setValue:[picker copy] forKey:NSStringFromSelector(@selector(setTitleTextAttributes:forState:))]; + [self.pickers setValue:dictionary forKey:key]; +} + +- (void)dk_setImage:(DKImagePicker)picker forState:(UIControlState)state { + + if (UIControlStateNormal == state) { + [self setImage:picker(self.dk_manager.themeVersion)]; + } else if (UIControlStateSelected == state) { + [self setSelectedImage:picker(self.dk_manager.themeVersion)]; + } + + NSString *key = [NSString stringWithFormat:@"%@", @(state)]; + NSMutableDictionary *dictionary = [self.pickers valueForKey:key]; + if (!dictionary) { + dictionary = [[NSMutableDictionary alloc] init]; + } + + if (UIControlStateNormal == state) { + [dictionary setValue:[picker copy] forKey:NSStringFromSelector(@selector(setImage:))]; + } else if (UIControlStateSelected == state) { + [dictionary setValue:[picker copy] forKey:NSStringFromSelector(@selector(setSelectedImage:))]; + } + [self.pickers setValue:dictionary forKey:key]; +} + +- (void)night_updateColor { + + [self.pickers enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { + if ([obj isKindOfClass:[NSDictionary class]]) { + NSDictionary *dictionary = (NSDictionary *)obj; + [dictionary enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull selector, DKAttributePicker _Nonnull picker, BOOL * _Nonnull stop) { + UIControlState state = [key integerValue]; + if ([selector isEqualToString:NSStringFromSelector(@selector(setTitleTextAttributes:forState:))]) { + NSDictionary *attribute = picker(self.dk_manager.themeVersion); + [self setTitleTextAttributes:attribute forState:state]; + } else if ([selector isEqualToString:NSStringFromSelector(@selector(setImage:))]) { + if (UIControlStateNormal == state) { + UIImage *resultImage = ((DKImagePicker)picker)(self.dk_manager.themeVersion); + [self setImage:resultImage]; + } + } else if ([selector isEqualToString:NSStringFromSelector(@selector(setSelectedImage:))]) { + if (UIControlStateSelected == state) { + UIImage *resultImage = ((DKImagePicker)picker)(self.dk_manager.themeVersion); + [self setSelectedImage:resultImage]; + } + } + }]; + } else { + SEL sel = NSSelectorFromString(key); + DKAttributePicker picker = (DKAttributePicker)obj; + NSDictionary *attribute = picker(self.dk_manager.themeVersion); + [UIView animateWithDuration:DKNightVersionAnimationDuration + animations:^{ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-performSelector-leaks" + [self performSelector:sel withObject:attribute]; +#pragma clang diagnostic pop + }]; + + } + }]; +} + +@end