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

Commit 6bb5106

Browse files
author
Bin Liu
committed
Replace ASButtonState with ASControlState
1 parent 8a5f080 commit 6bb5106

4 files changed

Lines changed: 57 additions & 26 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: 36 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 *_seletedAttributedTitle;
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 && _seletedAttributedTitle) {
92+
newTitle = _seletedAttributedTitle;
8793
} else {
8894
newTitle = _normalAttributedTitle;
8995
}
@@ -126,68 +132,82 @@ - (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 _seletedAttributedTitle;
147+
148+
case ASControlStateDisabled:
140149
return _disabledAttributedTitle;
141150
}
142151
}
143152

144-
- (void)setAttributedTitle:(NSAttributedString *)title forState:(ASButtonState)state
153+
- (void)setAttributedTitle:(NSAttributedString *)title forState:(ASControlState)state
145154
{
146155
ASDN::MutexLocker l(_propertyLock);
147156
switch (state) {
148-
case ASButtonStateNormal:
157+
case ASControlStateNormal:
149158
_normalAttributedTitle = [title copy];
150159
break;
151160

152-
case ASButtonStateHighlighted:
161+
case ASControlStateHighlighted:
153162
_highlightedAttributedTitle = [title copy];
154163
break;
155164

156-
case ASButtonStateDisabled:
165+
case ASControlStateSelected:
166+
_seletedAttributedTitle = [title copy];
167+
break;
168+
169+
case ASControlStateDisabled:
157170
_disabledAttributedTitle = [title copy];
158171
break;
159172
}
160173
[self updateTitle];
161174
}
162175

163-
- (UIImage *)imageForState:(ASButtonState)state
176+
- (UIImage *)imageForState:(ASControlState)state
164177
{
165178
ASDN::MutexLocker l(_propertyLock);
166179
switch (state) {
167-
case ASButtonStateNormal:
180+
case ASControlStateNormal:
168181
return _normalImage;
169182

170-
case ASButtonStateHighlighted:
183+
case ASControlStateHighlighted:
171184
return _highlightedImage;
172185

173-
case ASButtonStateDisabled:
186+
case ASControlStateSelected:
187+
return _selectedImage;
188+
189+
case ASControlStateDisabled:
174190
return _disabledImage;
175191
}
176192
}
177193

178-
- (void)setImage:(UIImage *)image forState:(ASButtonState)state
194+
- (void)setImage:(UIImage *)image forState:(ASControlState)state
179195
{
180196
ASDN::MutexLocker l(_propertyLock);
181197
switch (state) {
182-
case ASButtonStateNormal:
198+
case ASControlStateNormal:
183199
_normalImage = image;
184200
break;
185201

186-
case ASButtonStateHighlighted:
202+
case ASControlStateHighlighted:
187203
_highlightedImage = image;
188204
break;
189205

190-
case ASButtonStateDisabled:
206+
case ASControlStateSelected:
207+
_selectedImage = image;
208+
break;
209+
210+
case ASControlStateDisabled:
191211
_disabledImage = image;
192212
break;
193213
}

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=isSeleted) 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];

0 commit comments

Comments
 (0)