Skip to content

Commit 1bfcb29

Browse files
committed
- Adds window check when updating button attributes
1 parent ccde3b6 commit 1bfcb29

2 files changed

Lines changed: 124 additions & 81 deletions

File tree

Example/AXStateButtonExample/ViewController.m

Lines changed: 97 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
@interface ViewController ()
1313

1414
@property AXStateButton *materialButton;
15+
@property AXStateButton *tryAgainButton;
1516
@property AXStateButton *loadingButton;
1617

1718
@end
@@ -27,100 +28,131 @@ - (void)viewDidLoad {
2728
self.materialButton = [self createMaterialButton];
2829
[self.view addSubview:self.materialButton];
2930

31+
// Try again button
32+
self.tryAgainButton = [self createTryAgainButton];
33+
[self.view addSubview:self.tryAgainButton];
34+
3035
// Sample loading button
3136
self.loadingButton = [self createLoadingButton];
3237
[self.view addSubview:self.loadingButton];
3338
}
3439

3540
- (AXStateButton *)createMaterialButton {
36-
AXStateButton *materialButton = [AXStateButton button];
37-
materialButton.controlStateAnimationDuration = 0.1;
38-
[materialButton addTarget:self action:@selector(buttonTap:) forControlEvents:UIControlEventTouchUpInside];
41+
AXStateButton *button = [AXStateButton button];
42+
button.controlStateAnimationDuration = 0.1;
43+
[button addTarget:self action:@selector(buttonTap:) forControlEvents:UIControlEventTouchUpInside];
3944

40-
[materialButton setAttributedTitle:[[NSAttributedString alloc] initWithString:@"+"
41-
attributes:@{
42-
NSFontAttributeName: [UIFont boldSystemFontOfSize:28],
43-
NSForegroundColorAttributeName: [UIColor whiteColor]
44-
}]
45-
forState:UIControlStateNormal];
46-
[materialButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 4, 0)];
45+
[button setAttributedTitle:[[NSAttributedString alloc] initWithString:@"+"
46+
attributes:@{
47+
NSFontAttributeName: [UIFont boldSystemFontOfSize:28],
48+
NSForegroundColorAttributeName: [UIColor whiteColor]
49+
}]
50+
forState:UIControlStateNormal];
51+
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 4, 0)];
4752

48-
[materialButton setTransformRotationZ:0 forState:UIControlStateNormal];
49-
// [materialButton setTransformRotationZ:M_PI_4 forState:UIControlStateHighlighted];
53+
[button setTransformRotationZ:0 forState:UIControlStateNormal];
54+
// [button setTransformRotationZ:M_PI_4 forState:UIControlStateHighlighted];
5055

51-
[materialButton setTransformScale:1.0 forState:UIControlStateNormal];
52-
[materialButton setTransformScale:0.95 forState:UIControlStateHighlighted];
56+
[button setTransformScale:1.0 forState:UIControlStateNormal];
57+
[button setTransformScale:0.95 forState:UIControlStateHighlighted];
5358

54-
[materialButton setShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
55-
[materialButton setShadowOpacity:0.2 forState:UIControlStateNormal];
56-
[materialButton setShadowOffset:CGSizeMake(2, 2) forState:UIControlStateNormal];
57-
[materialButton setShadowOpacity:0.4 forState:UIControlStateHighlighted];
59+
[button setShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
60+
[button setShadowOpacity:0.2 forState:UIControlStateNormal];
61+
[button setShadowOffset:CGSizeMake(2, 2) forState:UIControlStateNormal];
62+
[button setShadowOpacity:0.4 forState:UIControlStateHighlighted];
5863

59-
[materialButton setBackgroundColor:[UIColor redColor] forState:UIControlStateNormal];
60-
[materialButton setBackgroundColor:[UIColor colorWithRed:216. / 255. green:0.0 blue:0.0 alpha:1.0]
61-
forState:UIControlStateHighlighted];
64+
[button setBackgroundColor:[UIColor redColor] forState:UIControlStateNormal];
65+
[button setBackgroundColor:[UIColor colorWithRed:216. / 255. green:0.0 blue:0.0 alpha:1.0]
66+
forState:UIControlStateHighlighted];
6267

6368
CGSize buttonSize = CGSizeMake(56, 56);
64-
[materialButton setCornerRadius:buttonSize.height / 2 forState:UIControlStateNormal];
65-
materialButton.frame = (CGRect){ CGPointZero, buttonSize };
69+
[button setCornerRadius:buttonSize.height / 2 forState:UIControlStateNormal];
70+
button.frame = (CGRect){ CGPointZero, buttonSize };
6671

67-
return materialButton;
72+
return button;
73+
}
74+
75+
- (AXStateButton *)createTryAgainButton {
76+
AXStateButton *button = [AXStateButton button];
77+
// [button addTarget:self action:@selector(buttonTap:) forControlEvents:UIControlEventTouchUpInside];
78+
79+
[button setTitle:@"Try again" forState:UIControlStateNormal];
80+
[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
81+
82+
[button setAlpha:1.0 forState:UIControlStateNormal];
83+
[button setAlpha:0.7 forState:UIControlStateHighlighted];
84+
85+
[button setTransformScale:1.0 forState:UIControlStateNormal];
86+
[button setTransformScale:0.95 forState:UIControlStateHighlighted];
87+
88+
[button setBorderWidth:1.0 forState:UIControlStateNormal];
89+
[button setBorderColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
90+
91+
[button setBackgroundColor:[UIColor clearColor] forState:UIControlStateNormal];
92+
93+
CGSize buttonSize = CGSizeMake(90, 30);
94+
[button setCornerRadius:6.0 forState:UIControlStateNormal];
95+
button.frame = (CGRect){ CGPointZero, buttonSize };
96+
97+
return button;
6898
}
6999

70100
- (AXStateButton *)createLoadingButton {
71-
AXStateButton *loadingButton = [AXStateButton button];
72-
loadingButton.controlStateAnimationDuration = 0.1;
73-
[loadingButton addTarget:self action:@selector(buttonTap:) forControlEvents:UIControlEventTouchUpInside];
74-
75-
[loadingButton setAttributedTitle:[[NSAttributedString alloc] initWithString:@"Load"
76-
attributes:@{
77-
NSFontAttributeName: [UIFont boldSystemFontOfSize:17],
78-
NSForegroundColorAttributeName: [UIColor whiteColor]
79-
}]
80-
forState:UIControlStateNormal];
81-
[loadingButton setAttributedTitle:[[NSAttributedString alloc] initWithString:@"Loading..."
82-
attributes:@{
83-
NSFontAttributeName: [UIFont boldSystemFontOfSize:17],
84-
NSForegroundColorAttributeName: [UIColor whiteColor]
85-
}]
86-
forState:UIControlStateDisabled];
87-
88-
[loadingButton setShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
89-
[loadingButton setShadowOpacity:0.2 forState:UIControlStateNormal];
90-
[loadingButton setShadowOffset:CGSizeMake(2, 2) forState:UIControlStateNormal];
91-
[loadingButton setShadowOpacity:0.4 forState:UIControlStateHighlighted];
92-
93-
[loadingButton setTransformRotationX:0 forState:UIControlStateNormal];
94-
[loadingButton setTransformRotationX:M_PI forState:UIControlStateDisabled];
95-
96-
[loadingButton setTransformRotationY:0 forState:UIControlStateNormal];
97-
[loadingButton setTransformRotationY:M_PI forState:UIControlStateDisabled];
98-
99-
[loadingButton setTransformRotationZ:0 forState:UIControlStateNormal];
100-
[loadingButton setTransformRotationZ:M_PI forState:UIControlStateDisabled];
101-
102-
[loadingButton setBackgroundColor:[UIColor purpleColor] forState:UIControlStateNormal];
103-
[loadingButton setBackgroundColor:[UIColor colorWithRed:102. / 255. green:0.0 blue:102. / 255. alpha:1.0]
104-
forState:UIControlStateHighlighted];
105-
[loadingButton setBackgroundColor:[UIColor grayColor] forState:UIControlStateDisabled];
101+
AXStateButton *button = [AXStateButton button];
102+
[button addTarget:self action:@selector(buttonTap:) forControlEvents:UIControlEventTouchUpInside];
103+
104+
[button setAttributedTitle:[[NSAttributedString alloc] initWithString:@"Load"
105+
attributes:@{
106+
NSFontAttributeName: [UIFont boldSystemFontOfSize:17],
107+
NSForegroundColorAttributeName: [UIColor whiteColor]
108+
}]
109+
forState:UIControlStateNormal];
110+
[button setAttributedTitle:[[NSAttributedString alloc] initWithString:@"Loading..."
111+
attributes:@{
112+
NSFontAttributeName: [UIFont boldSystemFontOfSize:17],
113+
NSForegroundColorAttributeName: [UIColor whiteColor]
114+
}]
115+
forState:UIControlStateDisabled];
116+
117+
[button setShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
118+
[button setShadowOpacity:0.2 forState:UIControlStateNormal];
119+
[button setShadowOffset:CGSizeMake(2, 2) forState:UIControlStateNormal];
120+
[button setShadowOpacity:0.4 forState:UIControlStateHighlighted];
121+
122+
[button setTransformRotationX:0 forState:UIControlStateNormal];
123+
[button setTransformRotationX:M_PI forState:UIControlStateDisabled];
124+
125+
[button setTransformRotationY:0 forState:UIControlStateNormal];
126+
[button setTransformRotationY:M_PI forState:UIControlStateDisabled];
127+
128+
[button setTransformRotationZ:0 forState:UIControlStateNormal];
129+
[button setTransformRotationZ:M_PI forState:UIControlStateDisabled];
130+
131+
[button setBackgroundColor:[UIColor purpleColor] forState:UIControlStateNormal];
132+
[button setBackgroundColor:[UIColor colorWithRed:102. / 255. green:0.0 blue:102. / 255. alpha:1.0]
133+
forState:UIControlStateHighlighted];
134+
[button setBackgroundColor:[UIColor grayColor] forState:UIControlStateDisabled];
106135

107136
CGSize buttonSize = CGSizeMake(100, 50);
108-
[loadingButton setCornerRadius:buttonSize.height / 2 forState:UIControlStateNormal];
109-
[loadingButton setCornerRadius:0 forState:UIControlStateDisabled];
110-
loadingButton.frame = (CGRect){ CGPointZero, buttonSize };
137+
[button setCornerRadius:buttonSize.height / 2 forState:UIControlStateNormal];
138+
[button setCornerRadius:0 forState:UIControlStateDisabled];
139+
button.frame = (CGRect){ CGPointZero, buttonSize };
111140

112-
return loadingButton;
141+
return button;
113142
}
114143

115144
- (void)viewWillLayoutSubviews {
116145
[super viewWillLayoutSubviews];
117146

118147
CGPoint materialButtonCenter = self.view.center;
119-
materialButtonCenter.y -= 50;
148+
materialButtonCenter.y -= 100;
120149
self.materialButton.center = materialButtonCenter;
121150

151+
CGPoint tryAgainButtonCenter = self.view.center;
152+
self.tryAgainButton.center = tryAgainButtonCenter;
153+
122154
CGPoint loadingButtonCenter = self.view.center;
123-
loadingButtonCenter.y += 50;
155+
loadingButtonCenter.y += 100;
124156
self.loadingButton.center = loadingButtonCenter;
125157
}
126158

Source/AXStateButton.m

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,21 @@ - (void)commonInit {
9898
self.stateDictionary = [stateDictionary copy];
9999
}
100100

101+
- (void)didMoveToWindow {
102+
[super didMoveToWindow];
103+
104+
if (self.window && self.superview) {
105+
BOOL animateControlStateChanges = self.animateControlStateChanges;
106+
self.animateControlStateChanges = NO;
107+
[self updateButtonState];
108+
self.animateControlStateChanges = animateControlStateChanges;
109+
}
110+
}
111+
101112
- (void)didMoveToSuperview {
102113
[super didMoveToSuperview];
103114

104-
if (self.superview) {
115+
if (self.window && self.superview) {
105116
BOOL animateControlStateChanges = self.animateControlStateChanges;
106117
self.animateControlStateChanges = NO;
107118
[self updateButtonState];
@@ -179,7 +190,7 @@ - (void)setTintColor:(UIColor *)tintColor forState:(UIControlState)controlState
179190
[self.stateDictionary[AXStateButtonTintColorKey] removeObjectForKey:@(controlState)];
180191
}
181192

182-
if (self.superview) {
193+
if (self.window && self.superview) {
183194
[self updateButtonState];
184195
}
185196
}
@@ -195,7 +206,7 @@ - (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)c
195206
[self.stateDictionary[AXStateButtonBackgroundColorKey] removeObjectForKey:@(controlState)];
196207
}
197208

198-
if (self.superview) {
209+
if (self.window && self.superview) {
199210
[self updateButtonState];
200211
}
201212
}
@@ -207,7 +218,7 @@ - (UIColor *)backgroundColorForState:(UIControlState)controlState {
207218
- (void)setAlpha:(CGFloat)alpha forState:(UIControlState)controlState {
208219
self.stateDictionary[AXStateButtonAlphaKey][@(controlState)] = @(alpha);
209220

210-
if (self.superview) {
221+
if (self.window && self.superview) {
211222
[self updateButtonState];
212223
}
213224
}
@@ -219,7 +230,7 @@ - (CGFloat)alphaForState:(UIControlState)controlState {
219230
- (void)setCornerRadius:(CGFloat)cornerRadius forState:(UIControlState)controlState {
220231
self.stateDictionary[AXStateButtonCornerRadiusKey][@(controlState)] = @(cornerRadius);
221232

222-
if (self.superview) {
233+
if (self.window && self.superview) {
223234
[self updateButtonState];
224235
}
225236
}
@@ -235,7 +246,7 @@ - (void)setBorderColor:(UIColor *)borderColor forState:(UIControlState)controlSt
235246
[self.stateDictionary[AXStateButtonBorderColorKey] removeObjectForKey:@(controlState)];
236247
}
237248

238-
if (self.superview) {
249+
if (self.window && self.superview) {
239250
[self updateButtonState];
240251
}
241252
}
@@ -247,7 +258,7 @@ - (UIColor *)borderColorForState:(UIControlState)controlState {
247258
- (void)setBorderWidth:(CGFloat)borderWidth forState:(UIControlState)controlState {
248259
self.stateDictionary[AXStateButtonBorderWidthKey][@(controlState)] = @(borderWidth);
249260

250-
if (self.superview) {
261+
if (self.window && self.superview) {
251262
[self updateButtonState];
252263
}
253264
}
@@ -259,7 +270,7 @@ - (CGFloat)borderWidthForState:(UIControlState)controlState {
259270
- (void)setTransformRotationX:(CGFloat)radians forState:(UIControlState)controlState {
260271
self.stateDictionary[AXStateButtonTransformRotationXKey][@(controlState)] = @(radians);
261272

262-
if (self.superview) {
273+
if (self.window && self.superview) {
263274
[self updateButtonState];
264275
}
265276
}
@@ -271,7 +282,7 @@ - (CGFloat)transformRotationXForState:(UIControlState)controlState {
271282
- (void)setTransformRotationY:(CGFloat)radians forState:(UIControlState)controlState {
272283
self.stateDictionary[AXStateButtonTransformRotationYKey][@(controlState)] = @(radians);
273284

274-
if (self.superview) {
285+
if (self.window && self.superview) {
275286
[self updateButtonState];
276287
}
277288
}
@@ -283,7 +294,7 @@ - (CGFloat)transformRotationYForState:(UIControlState)controlState {
283294
- (void)setTransformRotationZ:(CGFloat)radians forState:(UIControlState)controlState {
284295
self.stateDictionary[AXStateButtonTransformRotationZKey][@(controlState)] = @(radians);
285296

286-
if (self.superview) {
297+
if (self.window && self.superview) {
287298
[self updateButtonState];
288299
}
289300
}
@@ -295,7 +306,7 @@ - (CGFloat)transformRotationZForState:(UIControlState)controlState {
295306
- (void)setTransformScale:(CGFloat)scale forState:(UIControlState)controlState {
296307
self.stateDictionary[AXStateButtonTransformScaleKey][@(controlState)] = @(scale);
297308

298-
if (self.superview) {
309+
if (self.window && self.superview) {
299310
[self updateButtonState];
300311
}
301312
}
@@ -311,7 +322,7 @@ - (void)setShadowColor:(UIColor *)shadowColor forState:(UIControlState)controlSt
311322
[self.stateDictionary[AXStateButtonShadowColorKey] removeObjectForKey:@(controlState)];
312323
}
313324

314-
if (self.superview) {
325+
if (self.window && self.superview) {
315326
[self updateButtonState];
316327
}
317328
}
@@ -323,7 +334,7 @@ - (UIColor *)shadowColorForState:(UIControlState)controlState {
323334
- (void)setShadowOpacity:(CGFloat)shadowOpacity forState:(UIControlState)controlState {
324335
self.stateDictionary[AXStateButtonShadowOpacityKey][@(controlState)] = @(shadowOpacity);
325336

326-
if (self.superview) {
337+
if (self.window && self.superview) {
327338
[self updateButtonState];
328339
}
329340
}
@@ -335,7 +346,7 @@ - (CGFloat)shadowOpacityForState:(UIControlState)controlState {
335346
- (void)setShadowOffset:(CGSize)shadowOffset forState:(UIControlState)controlState {
336347
self.stateDictionary[AXStateButtonShadowOffsetKey][@(controlState)] = [NSValue valueWithCGSize:shadowOffset];
337348

338-
if (self.superview) {
349+
if (self.window && self.superview) {
339350
[self updateButtonState];
340351
}
341352
}
@@ -347,7 +358,7 @@ - (CGSize)shadowOffsetForState:(UIControlState)controlState {
347358
- (void)setShadowRadius:(CGFloat)shadowRadius forState:(UIControlState)controlState {
348359
self.stateDictionary[AXStateButtonShadowRadiusKey][@(controlState)] = @(shadowRadius);
349360

350-
if (self.superview) {
361+
if (self.window && self.superview) {
351362
[self updateButtonState];
352363
}
353364
}
@@ -363,7 +374,7 @@ - (void)setShadowPath:(UIBezierPath *)shadowPath forState:(UIControlState)contro
363374
[self.stateDictionary[AXStateButtonShadowPathKey] removeObjectForKey:@(controlState)];
364375
}
365376

366-
if (self.superview) {
377+
if (self.window && self.superview) {
367378
[self updateButtonState];
368379
}
369380
}

0 commit comments

Comments
 (0)