ASViewController example improvements (#2592)

This commit is contained in:
Michael Schneider
2016-11-10 16:33:01 -08:00
committed by appleguy
parent 171cc2f527
commit 2ba8239ff5
4 changed files with 24 additions and 32 deletions

View File

@@ -221,7 +221,7 @@ ASVisibilityDepthImplementation;
{ {
if (AS_AT_LEAST_IOS9) { if (AS_AT_LEAST_IOS9) {
CGSize viewSize = self.view.bounds.size; CGSize viewSize = self.view.bounds.size;
return ASSizeRangeMake(viewSize, viewSize); return ASSizeRangeMake(viewSize);
} else { } else {
return [self _legacyConstrainedSize]; return [self _legacyConstrainedSize];
} }

View File

@@ -27,9 +27,10 @@
self = [super init]; self = [super init];
if (self == nil) { return self; } if (self == nil) { return self; }
self.automaticallyManagesSubnodes = YES;
_imageNode = [[ASNetworkImageNode alloc] init]; _imageNode = [[ASNetworkImageNode alloc] init];
_imageNode.backgroundColor = ASDisplayNodeDefaultPlaceholderColor(); _imageNode.backgroundColor = ASDisplayNodeDefaultPlaceholderColor();
[self addSubnode:_imageNode];
return self; return self;
} }
@@ -38,7 +39,7 @@
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{ {
return [ASRatioLayoutSpec ratioLayoutSpecWithRatio:1.0 child:_imageNode]; return [ASRatioLayoutSpec ratioLayoutSpecWithRatio:1.0 child:self.imageNode];
} }
- (void)layoutDidFinish - (void)layoutDidFinish

View File

@@ -38,20 +38,20 @@ static const NSInteger kImageHeight = 200;
- (instancetype)initWithImageCategory:(NSString *)imageCategory - (instancetype)initWithImageCategory:(NSString *)imageCategory
{ {
self = [super init]; self = [super init];
if (self == nil) { return self; } if (self) {
// Enable automaticallyManagesSubnodes so the first time the layout pass of the node is happening all nodes that are referenced
_imageCategory = imageCategory; // in the laaout specification within layoutSpecThatFits: will be added automatically
self.automaticallyManagesSubnodes = YES;
_imageCategory = imageCategory;
// Create ASCollectionView. We don't have to add it explicitly as subnode as we will set usesImplicitHierarchyManagement to YES // Create ASCollectionView. We don't have to add it explicitly as subnode as we will set usesImplicitHierarchyManagement to YES
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
_collectionNode = [[ASCollectionNode alloc] initWithCollectionViewLayout:layout]; _collectionNode = [[ASCollectionNode alloc] initWithCollectionViewLayout:layout];
_collectionNode.delegate = self; _collectionNode.delegate = self;
_collectionNode.dataSource = self; _collectionNode.dataSource = self;
_collectionNode.backgroundColor = [UIColor whiteColor]; _collectionNode.backgroundColor = [UIColor whiteColor];
}
// Enable usesImplicitHierarchyManagement so the first time the layout pass of the node is happening all nodes that are referenced
// in layouts within layoutSpecThatFits: will be added automatically
self.automaticallyManagesSubnodes = YES;
return self; return self;
} }
@@ -66,9 +66,7 @@ static const NSInteger kImageHeight = 200;
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{ {
self.collectionNode.position = CGPointZero; return [ASWrapperLayoutSpec wrapperWithLayoutElement:self.collectionNode];
self.collectionNode.style.preferredSize = constrainedSize.max;
return [ASAbsoluteLayoutSpec absoluteLayoutSpecWithChildren:@[self.collectionNode]];
} }
#pragma mark - ASCollectionDataSource #pragma mark - ASCollectionDataSource

View File

@@ -45,8 +45,8 @@
- (void)dealloc - (void)dealloc
{ {
self.tableNode.delegate = nil; self.node.delegate = nil;
self.tableNode.dataSource = nil; self.node.dataSource = nil;
} }
@@ -58,23 +58,15 @@
self.title = @"Image Categories"; self.title = @"Image Categories";
self.tableNode.delegate = self; self.node.delegate = self;
self.tableNode.dataSource = self; self.node.dataSource = self;
} }
- (void)viewWillAppear:(BOOL)animated - (void)viewWillAppear:(BOOL)animated
{ {
[super viewWillAppear:animated]; [super viewWillAppear:animated];
[self.tableNode deselectRowAtIndexPath:self.tableNode.indexPathForSelectedRow animated:YES]; [self.node deselectRowAtIndexPath:self.node.indexPathForSelectedRow animated:YES];
}
#pragma mark - Setter / Getter
- (ASTableNode *)tableNode
{
return (ASTableNode *)self.node;
} }
@@ -87,6 +79,7 @@
- (ASCellNodeBlock)tableNode:(ASTableNode *)tableNode nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath - (ASCellNodeBlock)tableNode:(ASTableNode *)tableNode nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath
{ {
// As the block is executed on a background thread we need to cache the image category string outside
NSString *imageCategory = self.imageCategories[indexPath.row]; NSString *imageCategory = self.imageCategories[indexPath.row];
return ^{ return ^{
ASTextCellNode *textCellNode = [ASTextCellNode new]; ASTextCellNode *textCellNode = [ASTextCellNode new];