Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit fa9668a

Browse files
committed
Merge pull request #1038 from binl/bl_button_states
[ASButtonNode] Use ASControlState instead of declaring a unique ASButtonState type.
2 parents 8a5f080 + 6a4c953 commit fa9668a

5 files changed

Lines changed: 70 additions & 27 deletions

File tree

AsyncDisplayKit/ASButtonNode.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@
99
#import <AsyncDisplayKit/ASTextNode.h>
1010
#import <AsyncDisplayKit/ASImageNode.h>
1111

12-
typedef enum : NSUInteger {
13-
ASButtonStateNormal,
14-
ASButtonStateHighlighted,
15-
ASButtonStateDisabled,
16-
} ASButtonState;
17-
1812
@interface ASButtonNode : ASControlNode
1913

2014
@property (nonatomic, readonly) ASTextNode *titleNode;
@@ -43,10 +37,10 @@ typedef enum : NSUInteger {
4337
@property (nonatomic, assign) ASVerticalAlignment contentVerticalAlignment;
4438

4539

46-
- (NSAttributedString *)attributedTitleForState:(ASButtonState)state;
47-
- (void)setAttributedTitle:(NSAttributedString *)title forState:(ASButtonState)state;
40+
- (NSAttributedString *)attributedTitleForState:(ASControlState)state;
41+
- (void)setAttributedTitle:(NSAttributedString *)title forState:(ASControlState)state;
4842

49-
- (UIImage *)imageForState:(ASButtonState)state;
50-
- (void)setImage:(UIImage *)image forState:(ASButtonState)state;
43+
- (UIImage *)imageForState:(ASControlState)state;
44+
- (void)setImage:(UIImage *)image forState:(ASControlState)state;
5145

5246
@end

AsyncDisplayKit/ASButtonNode.mm

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ @interface ASButtonNode ()
1717

1818
NSAttributedString *_normalAttributedTitle;
1919
NSAttributedString *_highlightedAttributedTitle;
20+
NSAttributedString *_selectedAttributedTitle;
2021
NSAttributedString *_disabledAttributedTitle;
2122

2223
UIImage *_normalImage;
2324
UIImage *_highlightedImage;
25+
UIImage *_selectedImage;
2426
UIImage *_disabledImage;
2527
}
2628

@@ -66,6 +68,8 @@ - (void)updateImage
6668
newImage = _disabledImage;
6769
} else if (self.highlighted && _highlightedImage) {
6870
newImage = _highlightedImage;
71+
} else if (self.selected && _selectedImage) {
72+
newImage = _selectedImage;
6973
} else {
7074
newImage = _normalImage;
7175
}
@@ -84,6 +88,8 @@ - (void)updateTitle
8488
newTitle = _disabledAttributedTitle;
8589
} else if (self.highlighted && _highlightedAttributedTitle) {
8690
newTitle = _highlightedAttributedTitle;
91+
} else if (self.selected && _selectedAttributedTitle) {
92+
newTitle = _selectedAttributedTitle;
8793
} else {
8894
newTitle = _normalAttributedTitle;
8995
}
@@ -126,70 +132,96 @@ - (void)setLaysOutHorizontally:(BOOL)laysOutHorizontally
126132
[self setNeedsLayout];
127133
}
128134

129-
- (NSAttributedString *)attributedTitleForState:(ASButtonState)state
135+
- (NSAttributedString *)attributedTitleForState:(ASControlState)state
130136
{
131137
ASDN::MutexLocker l(_propertyLock);
132138
switch (state) {
133-
case ASButtonStateNormal:
139+
case ASControlStateNormal:
134140
return _normalAttributedTitle;
135141

136-
case ASButtonStateHighlighted:
142+
case ASControlStateHighlighted:
137143
return _highlightedAttributedTitle;
138144

139-
case ASButtonStateDisabled:
145+
case ASControlStateSelected:
146+
return _selectedAttributedTitle;
147+
148+
case ASControlStateDisabled:
140149
return _disabledAttributedTitle;
150+
151+
default:
152+
return _normalAttributedTitle;
141153
}
142154
}
143155

144-
- (void)setAttributedTitle:(NSAttributedString *)title forState:(ASButtonState)state
156+
- (void)setAttributedTitle:(NSAttributedString *)title forState:(ASControlState)state
145157
{
146158
ASDN::MutexLocker l(_propertyLock);
147159
switch (state) {
148-
case ASButtonStateNormal:
160+
case ASControlStateNormal:
149161
_normalAttributedTitle = [title copy];
150162
break;
151163

152-
case ASButtonStateHighlighted:
164+
case ASControlStateHighlighted:
153165
_highlightedAttributedTitle = [title copy];
154166
break;
155167

156-
case ASButtonStateDisabled:
168+
case ASControlStateSelected:
169+
_selectedAttributedTitle = [title copy];
170+
break;
171+
172+
case ASControlStateDisabled:
157173
_disabledAttributedTitle = [title copy];
158174
break;
175+
176+
default:
177+
break;
159178
}
160179
[self updateTitle];
161180
}
162181

163-
- (UIImage *)imageForState:(ASButtonState)state
182+
- (UIImage *)imageForState:(ASControlState)state
164183
{
165184
ASDN::MutexLocker l(_propertyLock);
166185
switch (state) {
167-
case ASButtonStateNormal:
186+
case ASControlStateNormal:
168187
return _normalImage;
169188

170-
case ASButtonStateHighlighted:
189+
case ASControlStateHighlighted:
171190
return _highlightedImage;
172191

173-
case ASButtonStateDisabled:
192+
case ASControlStateSelected:
193+
return _selectedImage;
194+
195+
case ASControlStateDisabled:
174196
return _disabledImage;
197+
198+
default:
199+
return _normalImage;
175200
}
176201
}
177202

178-
- (void)setImage:(UIImage *)image forState:(ASButtonState)state
203+
- (void)setImage:(UIImage *)image forState:(ASControlState)state
179204
{
180205
ASDN::MutexLocker l(_propertyLock);
181206
switch (state) {
182-
case ASButtonStateNormal:
207+
case ASControlStateNormal:
183208
_normalImage = image;
184209
break;
185210

186-
case ASButtonStateHighlighted:
211+
case ASControlStateHighlighted:
187212
_highlightedImage = image;
188213
break;
189214

190-
case ASButtonStateDisabled:
215+
case ASControlStateSelected:
216+
_selectedImage = image;
217+
break;
218+
219+
case ASControlStateDisabled:
191220
_disabledImage = image;
192221
break;
222+
223+
default:
224+
break;
193225
}
194226
[self updateImage];
195227
}

AsyncDisplayKit/ASControlNode.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ typedef NS_OPTIONS(NSUInteger, ASControlNodeEvent)
3434
ASControlNodeEventAllEvents = 0xFFFFFFFF
3535
};
3636

37+
typedef NS_OPTIONS(NSUInteger, ASControlState) {
38+
ASControlStateNormal = 0,
39+
ASControlStateHighlighted = 1 << 0, // used when ASControlNode isHighlighted is set
40+
ASControlStateDisabled = 1 << 1,
41+
ASControlStateSelected = 1 << 2, // used when ASControlNode isSeleted is set
42+
ASControlStateReserved = 0xFF000000 // flags reserved for internal framework use
43+
};
3744

3845
/**
3946
@abstract ASControlNode is the base class for control nodes (such as buttons), or nodes that track touches to invoke targets with action messages.
@@ -55,6 +62,12 @@ typedef NS_OPTIONS(NSUInteger, ASControlNodeEvent)
5562
*/
5663
@property (nonatomic, readonly, assign, getter=isHighlighted) BOOL highlighted;
5764

65+
/**
66+
@abstract Indicates whether or not the receiver is highlighted.
67+
@discussion This is set automatically when the receiver is tapped.
68+
*/
69+
@property (nonatomic, assign, getter=isSelected) BOOL selected;
70+
5871
#pragma mark - Tracking Touches
5972
/**
6073
@abstract Indicates whether or not the receiver is currently tracking touches related to an event.

AsyncDisplayKit/ASControlNode.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
186186
// Send the appropriate touch-up control event.
187187
CGRect expandedBounds = CGRectInset(self.view.bounds, kASControlNodeExpandedInset, kASControlNodeExpandedInset);
188188
BOOL touchUpIsInsideExpandedBounds = CGRectContainsPoint(expandedBounds, touchLocation);
189+
190+
if (touchUpIsInsideExpandedBounds) {
191+
self.selected = !self.selected;
192+
}
189193

190194
[self sendActionsForControlEvents:(touchUpIsInsideExpandedBounds ? ASControlNodeEventTouchUpInside : ASControlNodeEventTouchUpOutside)
191195
withEvent:event];

examples/Multiplex/Sample/ScreenNode.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ - (void)setText:(NSString *)text
6060
NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:22.0f]};
6161
NSAttributedString *string = [[NSAttributedString alloc] initWithString:text
6262
attributes:attributes];
63-
[_buttonNode setAttributedTitle:string forState:ASButtonStateNormal];
63+
[_buttonNode setAttributedTitle:string forState:ASControlStateNormal];
6464
[self setNeedsLayout];
6565
}
6666

0 commit comments

Comments
 (0)