UIButton+Night.m
4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// UIButton+Night.m
// DKNightVersion
//
// Created by Draveness on 15/12/9.
// Copyright © 2015年 DeltaX. All rights reserved.
//
#import "UIButton+Night.h"
#import <objc/runtime.h>
@interface UIButton ()
@property (nonatomic, strong) NSMutableDictionary<NSString *, id> *pickers;
@end
@implementation UIButton (Night)
- (void)dk_setTitleColorPicker:(DKColorPicker)picker forState:(UIControlState)state {
[self setTitleColor: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(setTitleColor:forState:))];
[self.pickers setValue:dictionary forKey:key];
}
- (void)dk_setBackgroundImage:(DKImagePicker)picker forState:(UIControlState)state {
[self setBackgroundImage: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(setBackgroundImage:forState:))];
[self.pickers setValue:dictionary forKey:key];
}
- (void)dk_setImage:(DKImagePicker)picker forState:(UIControlState)state {
[self setImage: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(setImage:forState:))];
[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<NSString *, DKColorPicker> *dictionary = (NSDictionary *)obj;
[dictionary enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull selector, DKColorPicker _Nonnull picker, BOOL * _Nonnull stop) {
UIControlState state = [key integerValue];
[UIView animateWithDuration:DKNightVersionAnimationDuration
animations:^{
if ([selector isEqualToString:NSStringFromSelector(@selector(setTitleColor:forState:))]) {
UIColor *resultColor = picker(self.dk_manager.themeVersion);
[self setTitleColor:resultColor forState:state];
} else if ([selector isEqualToString:NSStringFromSelector(@selector(setBackgroundImage:forState:))]) {
UIImage *resultImage = ((DKImagePicker)picker)(self.dk_manager.themeVersion);
[self setBackgroundImage:resultImage forState:state];
} else if ([selector isEqualToString:NSStringFromSelector(@selector(setImage:forState:))]) {
UIImage *resultImage = ((DKImagePicker)picker)(self.dk_manager.themeVersion);
[self setImage:resultImage forState:state];
}
}];
}];
} else {
SEL sel = NSSelectorFromString(key);
DKColorPicker picker = (DKColorPicker)obj;
UIColor *resultColor = picker(self.dk_manager.themeVersion);
[UIView animateWithDuration:DKNightVersionAnimationDuration
animations:^{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self performSelector:sel withObject:resultColor];
#pragma clang diagnostic pop
}];
}
}];
}
@end