Replace ASButtonState with ASControlState

This commit is contained in:
Bin Liu
2016-01-05 17:07:44 -08:00
parent 8a5f080fcc
commit 6bb51063f9
4 changed files with 57 additions and 26 deletions

View File

@@ -9,12 +9,6 @@
#import <AsyncDisplayKit/ASTextNode.h> #import <AsyncDisplayKit/ASTextNode.h>
#import <AsyncDisplayKit/ASImageNode.h> #import <AsyncDisplayKit/ASImageNode.h>
typedef enum : NSUInteger {
ASButtonStateNormal,
ASButtonStateHighlighted,
ASButtonStateDisabled,
} ASButtonState;
@interface ASButtonNode : ASControlNode @interface ASButtonNode : ASControlNode
@property (nonatomic, readonly) ASTextNode *titleNode; @property (nonatomic, readonly) ASTextNode *titleNode;
@@ -43,10 +37,10 @@ typedef enum : NSUInteger {
@property (nonatomic, assign) ASVerticalAlignment contentVerticalAlignment; @property (nonatomic, assign) ASVerticalAlignment contentVerticalAlignment;
- (NSAttributedString *)attributedTitleForState:(ASButtonState)state; - (NSAttributedString *)attributedTitleForState:(ASControlState)state;
- (void)setAttributedTitle:(NSAttributedString *)title forState:(ASButtonState)state; - (void)setAttributedTitle:(NSAttributedString *)title forState:(ASControlState)state;
- (UIImage *)imageForState:(ASButtonState)state; - (UIImage *)imageForState:(ASControlState)state;
- (void)setImage:(UIImage *)image forState:(ASButtonState)state; - (void)setImage:(UIImage *)image forState:(ASControlState)state;
@end @end

View File

@@ -17,10 +17,12 @@
NSAttributedString *_normalAttributedTitle; NSAttributedString *_normalAttributedTitle;
NSAttributedString *_highlightedAttributedTitle; NSAttributedString *_highlightedAttributedTitle;
NSAttributedString *_seletedAttributedTitle;
NSAttributedString *_disabledAttributedTitle; NSAttributedString *_disabledAttributedTitle;
UIImage *_normalImage; UIImage *_normalImage;
UIImage *_highlightedImage; UIImage *_highlightedImage;
UIImage *_selectedImage;
UIImage *_disabledImage; UIImage *_disabledImage;
} }
@@ -66,6 +68,8 @@
newImage = _disabledImage; newImage = _disabledImage;
} else if (self.highlighted && _highlightedImage) { } else if (self.highlighted && _highlightedImage) {
newImage = _highlightedImage; newImage = _highlightedImage;
} else if (self.selected && _selectedImage) {
newImage = _selectedImage;
} else { } else {
newImage = _normalImage; newImage = _normalImage;
} }
@@ -84,6 +88,8 @@
newTitle = _disabledAttributedTitle; newTitle = _disabledAttributedTitle;
} else if (self.highlighted && _highlightedAttributedTitle) { } else if (self.highlighted && _highlightedAttributedTitle) {
newTitle = _highlightedAttributedTitle; newTitle = _highlightedAttributedTitle;
} else if (self.selected && _seletedAttributedTitle) {
newTitle = _seletedAttributedTitle;
} else { } else {
newTitle = _normalAttributedTitle; newTitle = _normalAttributedTitle;
} }
@@ -126,68 +132,82 @@
[self setNeedsLayout]; [self setNeedsLayout];
} }
- (NSAttributedString *)attributedTitleForState:(ASButtonState)state - (NSAttributedString *)attributedTitleForState:(ASControlState)state
{ {
ASDN::MutexLocker l(_propertyLock); ASDN::MutexLocker l(_propertyLock);
switch (state) { switch (state) {
case ASButtonStateNormal: case ASControlStateNormal:
return _normalAttributedTitle; return _normalAttributedTitle;
case ASButtonStateHighlighted: case ASControlStateHighlighted:
return _highlightedAttributedTitle; return _highlightedAttributedTitle;
case ASButtonStateDisabled: case ASControlStateSelected:
return _seletedAttributedTitle;
case ASControlStateDisabled:
return _disabledAttributedTitle; return _disabledAttributedTitle;
} }
} }
- (void)setAttributedTitle:(NSAttributedString *)title forState:(ASButtonState)state - (void)setAttributedTitle:(NSAttributedString *)title forState:(ASControlState)state
{ {
ASDN::MutexLocker l(_propertyLock); ASDN::MutexLocker l(_propertyLock);
switch (state) { switch (state) {
case ASButtonStateNormal: case ASControlStateNormal:
_normalAttributedTitle = [title copy]; _normalAttributedTitle = [title copy];
break; break;
case ASButtonStateHighlighted: case ASControlStateHighlighted:
_highlightedAttributedTitle = [title copy]; _highlightedAttributedTitle = [title copy];
break; break;
case ASButtonStateDisabled: case ASControlStateSelected:
_seletedAttributedTitle = [title copy];
break;
case ASControlStateDisabled:
_disabledAttributedTitle = [title copy]; _disabledAttributedTitle = [title copy];
break; break;
} }
[self updateTitle]; [self updateTitle];
} }
- (UIImage *)imageForState:(ASButtonState)state - (UIImage *)imageForState:(ASControlState)state
{ {
ASDN::MutexLocker l(_propertyLock); ASDN::MutexLocker l(_propertyLock);
switch (state) { switch (state) {
case ASButtonStateNormal: case ASControlStateNormal:
return _normalImage; return _normalImage;
case ASButtonStateHighlighted: case ASControlStateHighlighted:
return _highlightedImage; return _highlightedImage;
case ASButtonStateDisabled: case ASControlStateSelected:
return _selectedImage;
case ASControlStateDisabled:
return _disabledImage; return _disabledImage;
} }
} }
- (void)setImage:(UIImage *)image forState:(ASButtonState)state - (void)setImage:(UIImage *)image forState:(ASControlState)state
{ {
ASDN::MutexLocker l(_propertyLock); ASDN::MutexLocker l(_propertyLock);
switch (state) { switch (state) {
case ASButtonStateNormal: case ASControlStateNormal:
_normalImage = image; _normalImage = image;
break; break;
case ASButtonStateHighlighted: case ASControlStateHighlighted:
_highlightedImage = image; _highlightedImage = image;
break; break;
case ASButtonStateDisabled: case ASControlStateSelected:
_selectedImage = image;
break;
case ASControlStateDisabled:
_disabledImage = image; _disabledImage = image;
break; break;
} }

View File

@@ -34,6 +34,13 @@ typedef NS_OPTIONS(NSUInteger, ASControlNodeEvent)
ASControlNodeEventAllEvents = 0xFFFFFFFF 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. @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; @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 #pragma mark - Tracking Touches
/** /**
@abstract Indicates whether or not the receiver is currently tracking touches related to an event. @abstract Indicates whether or not the receiver is currently tracking touches related to an event.

View File

@@ -187,6 +187,10 @@ void _ASEnumerateControlEventsIncludedInMaskWithBlock(ASControlNodeEvent mask, v
CGRect expandedBounds = CGRectInset(self.view.bounds, kASControlNodeExpandedInset, kASControlNodeExpandedInset); CGRect expandedBounds = CGRectInset(self.view.bounds, kASControlNodeExpandedInset, kASControlNodeExpandedInset);
BOOL touchUpIsInsideExpandedBounds = CGRectContainsPoint(expandedBounds, touchLocation); BOOL touchUpIsInsideExpandedBounds = CGRectContainsPoint(expandedBounds, touchLocation);
if (touchUpIsInsideExpandedBounds) {
self.selected = !self.selected;
}
[self sendActionsForControlEvents:(touchUpIsInsideExpandedBounds ? ASControlNodeEventTouchUpInside : ASControlNodeEventTouchUpOutside) [self sendActionsForControlEvents:(touchUpIsInsideExpandedBounds ? ASControlNodeEventTouchUpInside : ASControlNodeEventTouchUpOutside)
withEvent:event]; withEvent:event];
} }