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/ASImageNode.h>
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

View File

@@ -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;
}

View File

@@ -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.

View File

@@ -187,6 +187,10 @@ void _ASEnumerateControlEventsIncludedInMaskWithBlock(ASControlNodeEvent mask, v
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];
}