From 6bb51063f9ae4658a5869ba282f9ca2d42d750e6 Mon Sep 17 00:00:00 2001 From: Bin Liu Date: Tue, 5 Jan 2016 17:07:44 -0800 Subject: [PATCH] Replace ASButtonState with ASControlState --- AsyncDisplayKit/ASButtonNode.h | 14 +++------ AsyncDisplayKit/ASButtonNode.mm | 52 +++++++++++++++++++++++---------- AsyncDisplayKit/ASControlNode.h | 13 +++++++++ AsyncDisplayKit/ASControlNode.m | 4 +++ 4 files changed, 57 insertions(+), 26 deletions(-) diff --git a/AsyncDisplayKit/ASButtonNode.h b/AsyncDisplayKit/ASButtonNode.h index 8156496279..588319b4b6 100644 --- a/AsyncDisplayKit/ASButtonNode.h +++ b/AsyncDisplayKit/ASButtonNode.h @@ -9,12 +9,6 @@ #import #import -typedef enum : NSUInteger { - ASButtonStateNormal, - ASButtonStateHighlighted, - ASButtonStateDisabled, -} ASButtonState; - @interface ASButtonNode : ASControlNode @property (nonatomic, readonly) ASTextNode *titleNode; @@ -43,10 +37,10 @@ typedef enum : NSUInteger { @property (nonatomic, assign) ASVerticalAlignment contentVerticalAlignment; -- (NSAttributedString *)attributedTitleForState:(ASButtonState)state; -- (void)setAttributedTitle:(NSAttributedString *)title forState:(ASButtonState)state; +- (NSAttributedString *)attributedTitleForState:(ASControlState)state; +- (void)setAttributedTitle:(NSAttributedString *)title forState:(ASControlState)state; -- (UIImage *)imageForState:(ASButtonState)state; -- (void)setImage:(UIImage *)image forState:(ASButtonState)state; +- (UIImage *)imageForState:(ASControlState)state; +- (void)setImage:(UIImage *)image forState:(ASControlState)state; @end diff --git a/AsyncDisplayKit/ASButtonNode.mm b/AsyncDisplayKit/ASButtonNode.mm index 0d4004411c..6d814a6804 100644 --- a/AsyncDisplayKit/ASButtonNode.mm +++ b/AsyncDisplayKit/ASButtonNode.mm @@ -17,10 +17,12 @@ NSAttributedString *_normalAttributedTitle; NSAttributedString *_highlightedAttributedTitle; + NSAttributedString *_seletedAttributedTitle; NSAttributedString *_disabledAttributedTitle; UIImage *_normalImage; UIImage *_highlightedImage; + UIImage *_selectedImage; UIImage *_disabledImage; } @@ -66,6 +68,8 @@ newImage = _disabledImage; } else if (self.highlighted && _highlightedImage) { newImage = _highlightedImage; + } else if (self.selected && _selectedImage) { + newImage = _selectedImage; } else { newImage = _normalImage; } @@ -84,6 +88,8 @@ newTitle = _disabledAttributedTitle; } else if (self.highlighted && _highlightedAttributedTitle) { newTitle = _highlightedAttributedTitle; + } else if (self.selected && _seletedAttributedTitle) { + newTitle = _seletedAttributedTitle; } else { newTitle = _normalAttributedTitle; } @@ -126,68 +132,82 @@ [self setNeedsLayout]; } -- (NSAttributedString *)attributedTitleForState:(ASButtonState)state +- (NSAttributedString *)attributedTitleForState:(ASControlState)state { ASDN::MutexLocker l(_propertyLock); switch (state) { - case ASButtonStateNormal: + case ASControlStateNormal: return _normalAttributedTitle; - case ASButtonStateHighlighted: + case ASControlStateHighlighted: return _highlightedAttributedTitle; - case ASButtonStateDisabled: + case ASControlStateSelected: + return _seletedAttributedTitle; + + case ASControlStateDisabled: return _disabledAttributedTitle; } } -- (void)setAttributedTitle:(NSAttributedString *)title forState:(ASButtonState)state +- (void)setAttributedTitle:(NSAttributedString *)title forState:(ASControlState)state { ASDN::MutexLocker l(_propertyLock); switch (state) { - case ASButtonStateNormal: + case ASControlStateNormal: _normalAttributedTitle = [title copy]; break; - case ASButtonStateHighlighted: + case ASControlStateHighlighted: _highlightedAttributedTitle = [title copy]; break; - case ASButtonStateDisabled: + case ASControlStateSelected: + _seletedAttributedTitle = [title copy]; + break; + + case ASControlStateDisabled: _disabledAttributedTitle = [title copy]; break; } [self updateTitle]; } -- (UIImage *)imageForState:(ASButtonState)state +- (UIImage *)imageForState:(ASControlState)state { ASDN::MutexLocker l(_propertyLock); switch (state) { - case ASButtonStateNormal: + case ASControlStateNormal: return _normalImage; - case ASButtonStateHighlighted: + case ASControlStateHighlighted: return _highlightedImage; - case ASButtonStateDisabled: + case ASControlStateSelected: + return _selectedImage; + + case ASControlStateDisabled: return _disabledImage; } } -- (void)setImage:(UIImage *)image forState:(ASButtonState)state +- (void)setImage:(UIImage *)image forState:(ASControlState)state { ASDN::MutexLocker l(_propertyLock); switch (state) { - case ASButtonStateNormal: + case ASControlStateNormal: _normalImage = image; break; - case ASButtonStateHighlighted: + case ASControlStateHighlighted: _highlightedImage = image; break; - case ASButtonStateDisabled: + case ASControlStateSelected: + _selectedImage = image; + break; + + case ASControlStateDisabled: _disabledImage = image; break; } diff --git a/AsyncDisplayKit/ASControlNode.h b/AsyncDisplayKit/ASControlNode.h index 28a40ad51e..ad7049c261 100644 --- a/AsyncDisplayKit/ASControlNode.h +++ b/AsyncDisplayKit/ASControlNode.h @@ -34,6 +34,13 @@ typedef NS_OPTIONS(NSUInteger, ASControlNodeEvent) ASControlNodeEventAllEvents = 0xFFFFFFFF }; +typedef NS_OPTIONS(NSUInteger, ASControlState) { + ASControlStateNormal = 0, + ASControlStateHighlighted = 1 << 0, // used when ASControlNode isHighlighted is set + ASControlStateDisabled = 1 << 1, + ASControlStateSelected = 1 << 2, // used when ASControlNode isSeleted is set + ASControlStateReserved = 0xFF000000 // flags reserved for internal framework use +}; /** @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) */ @property (nonatomic, readonly, assign, getter=isHighlighted) BOOL highlighted; +/** + @abstract Indicates whether or not the receiver is highlighted. + @discussion This is set automatically when the receiver is tapped. + */ +@property (nonatomic, assign, getter=isSeleted) BOOL selected; + #pragma mark - Tracking Touches /** @abstract Indicates whether or not the receiver is currently tracking touches related to an event. diff --git a/AsyncDisplayKit/ASControlNode.m b/AsyncDisplayKit/ASControlNode.m index 0e2ab78d5e..6a9c8bd250 100644 --- a/AsyncDisplayKit/ASControlNode.m +++ b/AsyncDisplayKit/ASControlNode.m @@ -186,6 +186,10 @@ void _ASEnumerateControlEventsIncludedInMaskWithBlock(ASControlNodeEvent mask, v // Send the appropriate touch-up control event. CGRect expandedBounds = CGRectInset(self.view.bounds, kASControlNodeExpandedInset, kASControlNodeExpandedInset); BOOL touchUpIsInsideExpandedBounds = CGRectContainsPoint(expandedBounds, touchLocation); + + if (touchUpIsInsideExpandedBounds) { + self.selected = !self.selected; + } [self sendActionsForControlEvents:(touchUpIsInsideExpandedBounds ? ASControlNodeEventTouchUpInside : ASControlNodeEventTouchUpOutside) withEvent:event];