[ASButtonNode] lazily initialize label, image, and backgroundImage (skip if never needed).

This commit is contained in:
Scott Goodson 2016-02-20 13:00:01 -08:00
parent c635ceb9db
commit cd6ca2885e

View File

@ -12,6 +12,7 @@
#import "ASDisplayNode+Subclasses.h" #import "ASDisplayNode+Subclasses.h"
#import "ASBackgroundLayoutSpec.h" #import "ASBackgroundLayoutSpec.h"
#import "ASInsetLayoutSpec.h" #import "ASInsetLayoutSpec.h"
#import "ASDisplayNode+Beta.h"
@interface ASButtonNode () @interface ASButtonNode ()
{ {
@ -42,35 +43,53 @@
@synthesize contentVerticalAlignment = _contentVerticalAlignment; @synthesize contentVerticalAlignment = _contentVerticalAlignment;
@synthesize contentHorizontalAlignment = _contentHorizontalAlignment; @synthesize contentHorizontalAlignment = _contentHorizontalAlignment;
@synthesize contentEdgeInsets = _contentEdgeInsets; @synthesize contentEdgeInsets = _contentEdgeInsets;
@synthesize titleNode = _titleNode;
@synthesize imageNode = _imageNode;
@synthesize backgroundImageNode = _backgroundImageNode;
- (instancetype)init - (instancetype)init
{ {
if (self = [super init]) { if (self = [super init]) {
self.usesImplicitHierarchyManagement = YES;
_contentSpacing = 8.0; _contentSpacing = 8.0;
_laysOutHorizontally = YES; _laysOutHorizontally = YES;
_titleNode = [[ASTextNode alloc] init];
_imageNode = [[ASImageNode alloc] init];
_backgroundImageNode = [[ASImageNode alloc] init];
[_backgroundImageNode setContentMode:UIViewContentModeScaleToFill];
[_titleNode setLayerBacked:YES];
[_imageNode setLayerBacked:YES];
[_backgroundImageNode setLayerBacked:YES];
[_titleNode setFlexShrink:YES];
_contentHorizontalAlignment = ASAlignmentMiddle; _contentHorizontalAlignment = ASAlignmentMiddle;
_contentVerticalAlignment = ASAlignmentCenter; _contentVerticalAlignment = ASAlignmentCenter;
_contentEdgeInsets = UIEdgeInsetsZero; _contentEdgeInsets = UIEdgeInsetsZero;
[self addSubnode:_backgroundImageNode];
[self addSubnode:_titleNode];
[self addSubnode:_imageNode];
} }
return self; return self;
} }
- (ASTextNode *)titleNode
{
if (!_titleNode) {
_titleNode = [[ASTextNode alloc] init];
[_titleNode setLayerBacked:YES];
}
return _titleNode;
}
- (ASImageNode *)imageNode
{
if (!_imageNode) {
_imageNode = [[ASImageNode alloc] init];
[_imageNode setLayerBacked:YES];
[_titleNode setFlexShrink:YES];
}
return _imageNode;
}
- (ASImageNode *)backgroundImageNode
{
if (!_backgroundImageNode) {
_backgroundImageNode = [[ASImageNode alloc] init];
[_backgroundImageNode setLayerBacked:YES];
[_backgroundImageNode setContentMode:UIViewContentModeScaleToFill];
}
return _backgroundImageNode;
}
- (void)setLayerBacked:(BOOL)layerBacked - (void)setLayerBacked:(BOOL)layerBacked
{ {
ASDisplayNodeAssert(!layerBacked, @"ASButtonNode must not be layer backed!"); ASDisplayNodeAssert(!layerBacked, @"ASButtonNode must not be layer backed!");
@ -105,6 +124,7 @@
- (void)setDisplaysAsynchronously:(BOOL)displaysAsynchronously - (void)setDisplaysAsynchronously:(BOOL)displaysAsynchronously
{ {
[super setDisplaysAsynchronously:displaysAsynchronously]; [super setDisplaysAsynchronously:displaysAsynchronously];
[self.backgroundImageNode setDisplaysAsynchronously:displaysAsynchronously];
[self.imageNode setDisplaysAsynchronously:displaysAsynchronously]; [self.imageNode setDisplaysAsynchronously:displaysAsynchronously];
[self.titleNode setDisplaysAsynchronously:displaysAsynchronously]; [self.titleNode setDisplaysAsynchronously:displaysAsynchronously];
} }
@ -124,8 +144,8 @@
newImage = _normalImage; newImage = _normalImage;
} }
if (newImage != self.imageNode.image) { if ((_imageNode != nil || newImage != nil) && newImage != self.imageNode.image) {
self.imageNode.image = newImage; _imageNode.image = newImage;
[self setNeedsLayout]; [self setNeedsLayout];
} }
} }
@ -144,8 +164,8 @@
newTitle = _normalAttributedTitle; newTitle = _normalAttributedTitle;
} }
if (newTitle != self.titleNode.attributedString) { if ((_titleNode != nil || newTitle.length > 0) && newTitle != self.titleNode.attributedString) {
self.titleNode.attributedString = newTitle; _titleNode.attributedString = newTitle;
[self setNeedsLayout]; [self setNeedsLayout];
} }
} }
@ -165,8 +185,8 @@
newImage = _normalBackgroundImage; newImage = _normalBackgroundImage;
} }
if (newImage != self.backgroundImageNode.image) { if ((_backgroundImageNode != nil || newImage != nil) && newImage != self.backgroundImageNode.image) {
self.backgroundImageNode.image = newImage; _backgroundImageNode.image = newImage;
[self setNeedsLayout]; [self setNeedsLayout];
} }
} }
@ -409,12 +429,12 @@
} }
NSMutableArray *children = [[NSMutableArray alloc] initWithCapacity:2]; NSMutableArray *children = [[NSMutableArray alloc] initWithCapacity:2];
if (self.imageNode.image) { if (_imageNode.image) {
[children addObject:self.imageNode]; [children addObject:_imageNode];
} }
if (self.titleNode.attributedString.length > 0) { if (_titleNode.attributedString.length > 0) {
[children addObject:self.titleNode]; [children addObject:_titleNode];
} }
stack.children = children; stack.children = children;
@ -425,9 +445,9 @@
spec = [ASInsetLayoutSpec insetLayoutSpecWithInsets:contentEdgeInsets child:spec]; spec = [ASInsetLayoutSpec insetLayoutSpecWithInsets:contentEdgeInsets child:spec];
} }
if (self.backgroundImageNode.image) { if (_backgroundImageNode.image) {
spec = [ASBackgroundLayoutSpec backgroundLayoutSpecWithChild:spec spec = [ASBackgroundLayoutSpec backgroundLayoutSpecWithChild:spec
background:self.backgroundImageNode]; background:_backgroundImageNode];
} }
return spec; return spec;
@ -436,9 +456,9 @@
- (void)layout - (void)layout
{ {
[super layout]; [super layout];
self.backgroundImageNode.hidden = self.backgroundImageNode.image == nil; _backgroundImageNode.hidden = (_backgroundImageNode.image == nil);
self.imageNode.hidden = self.imageNode.image == nil; _imageNode.hidden = (_imageNode.image == nil);
self.titleNode.hidden = self.titleNode.attributedString.length > 0 == NO; _titleNode.hidden = (_titleNode.attributedString.length == 0);
} }
@end @end