From 2ba8239ff5273e4f05a70cc1c577936bb5110c68 Mon Sep 17 00:00:00 2001 From: Michael Schneider Date: Thu, 10 Nov 2016 16:33:01 -0800 Subject: [PATCH] ASViewController example improvements (#2592) --- AsyncDisplayKit/ASViewController.mm | 2 +- .../ASViewController/Sample/DetailCellNode.m | 5 ++-- .../ASViewController/Sample/DetailRootNode.m | 30 +++++++++---------- .../ASViewController/Sample/ViewController.m | 19 ++++-------- 4 files changed, 24 insertions(+), 32 deletions(-) diff --git a/AsyncDisplayKit/ASViewController.mm b/AsyncDisplayKit/ASViewController.mm index 67aa677db7..dfb41bfc7f 100644 --- a/AsyncDisplayKit/ASViewController.mm +++ b/AsyncDisplayKit/ASViewController.mm @@ -221,7 +221,7 @@ ASVisibilityDepthImplementation; { if (AS_AT_LEAST_IOS9) { CGSize viewSize = self.view.bounds.size; - return ASSizeRangeMake(viewSize, viewSize); + return ASSizeRangeMake(viewSize); } else { return [self _legacyConstrainedSize]; } diff --git a/examples/ASViewController/Sample/DetailCellNode.m b/examples/ASViewController/Sample/DetailCellNode.m index 283e253acb..d069f75c18 100644 --- a/examples/ASViewController/Sample/DetailCellNode.m +++ b/examples/ASViewController/Sample/DetailCellNode.m @@ -27,9 +27,10 @@ self = [super init]; if (self == nil) { return self; } + self.automaticallyManagesSubnodes = YES; + _imageNode = [[ASNetworkImageNode alloc] init]; _imageNode.backgroundColor = ASDisplayNodeDefaultPlaceholderColor(); - [self addSubnode:_imageNode]; return self; } @@ -38,7 +39,7 @@ - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize { - return [ASRatioLayoutSpec ratioLayoutSpecWithRatio:1.0 child:_imageNode]; + return [ASRatioLayoutSpec ratioLayoutSpecWithRatio:1.0 child:self.imageNode]; } - (void)layoutDidFinish diff --git a/examples/ASViewController/Sample/DetailRootNode.m b/examples/ASViewController/Sample/DetailRootNode.m index 21474acfd5..333af36e6c 100644 --- a/examples/ASViewController/Sample/DetailRootNode.m +++ b/examples/ASViewController/Sample/DetailRootNode.m @@ -38,20 +38,20 @@ static const NSInteger kImageHeight = 200; - (instancetype)initWithImageCategory:(NSString *)imageCategory { self = [super init]; - if (self == nil) { return self; } - - _imageCategory = imageCategory; + if (self) { + // Enable automaticallyManagesSubnodes so the first time the layout pass of the node is happening all nodes that are referenced + // 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 - UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; - _collectionNode = [[ASCollectionNode alloc] initWithCollectionViewLayout:layout]; - _collectionNode.delegate = self; - _collectionNode.dataSource = self; - _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; + // Create ASCollectionView. We don't have to add it explicitly as subnode as we will set usesImplicitHierarchyManagement to YES + UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; + _collectionNode = [[ASCollectionNode alloc] initWithCollectionViewLayout:layout]; + _collectionNode.delegate = self; + _collectionNode.dataSource = self; + _collectionNode.backgroundColor = [UIColor whiteColor]; + } return self; } @@ -66,9 +66,7 @@ static const NSInteger kImageHeight = 200; - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize { - self.collectionNode.position = CGPointZero; - self.collectionNode.style.preferredSize = constrainedSize.max; - return [ASAbsoluteLayoutSpec absoluteLayoutSpecWithChildren:@[self.collectionNode]]; + return [ASWrapperLayoutSpec wrapperWithLayoutElement:self.collectionNode]; } #pragma mark - ASCollectionDataSource diff --git a/examples/ASViewController/Sample/ViewController.m b/examples/ASViewController/Sample/ViewController.m index 04b03c72af..9e70c99c54 100644 --- a/examples/ASViewController/Sample/ViewController.m +++ b/examples/ASViewController/Sample/ViewController.m @@ -45,8 +45,8 @@ - (void)dealloc { - self.tableNode.delegate = nil; - self.tableNode.dataSource = nil; + self.node.delegate = nil; + self.node.dataSource = nil; } @@ -58,23 +58,15 @@ self.title = @"Image Categories"; - self.tableNode.delegate = self; - self.tableNode.dataSource = self; + self.node.delegate = self; + self.node.dataSource = self; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; - [self.tableNode deselectRowAtIndexPath:self.tableNode.indexPathForSelectedRow animated:YES]; -} - - -#pragma mark - Setter / Getter - -- (ASTableNode *)tableNode -{ - return (ASTableNode *)self.node; + [self.node deselectRowAtIndexPath:self.node.indexPathForSelectedRow animated:YES]; } @@ -87,6 +79,7 @@ - (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]; return ^{ ASTextCellNode *textCellNode = [ASTextCellNode new];