From 788cdbd326e5d6b063e22deb543cd5c1eb377a1c Mon Sep 17 00:00:00 2001 From: Michael Schneider Date: Wed, 16 Mar 2016 09:36:35 -0700 Subject: [PATCH] Improve ASViewController example --- examples/ASViewController/Podfile | 2 +- .../ASViewController/Sample/AppDelegate.m | 23 ----------------- .../ASViewController/Sample/DetailCellNode.m | 22 +++++++--------- .../ASViewController/Sample/DetailRootNode.m | 25 +++++++++---------- .../Sample/DetailViewController.m | 2 +- 5 files changed, 23 insertions(+), 51 deletions(-) diff --git a/examples/ASViewController/Podfile b/examples/ASViewController/Podfile index 6c012e3c04..840a147de4 100644 --- a/examples/ASViewController/Podfile +++ b/examples/ASViewController/Podfile @@ -1,3 +1,3 @@ source 'https://github.com/CocoaPods/Specs.git' -platform :ios, '8.0' +platform :ios, '7.1' pod 'AsyncDisplayKit', :path => '../..' diff --git a/examples/ASViewController/Sample/AppDelegate.m b/examples/ASViewController/Sample/AppDelegate.m index 52ce975a1b..82398fb14a 100644 --- a/examples/ASViewController/Sample/AppDelegate.m +++ b/examples/ASViewController/Sample/AppDelegate.m @@ -18,7 +18,6 @@ @implementation AppDelegate - - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; @@ -28,26 +27,4 @@ return YES; } -- (void)applicationWillResignActive:(UIApplication *)application { - // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. - // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. -} - -- (void)applicationDidEnterBackground:(UIApplication *)application { - // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. - // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. -} - -- (void)applicationWillEnterForeground:(UIApplication *)application { - // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. -} - -- (void)applicationDidBecomeActive:(UIApplication *)application { - // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. -} - -- (void)applicationWillTerminate:(UIApplication *)application { - // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. -} - @end diff --git a/examples/ASViewController/Sample/DetailCellNode.m b/examples/ASViewController/Sample/DetailCellNode.m index 825bb657ac..a5e99653ea 100644 --- a/examples/ASViewController/Sample/DetailCellNode.m +++ b/examples/ASViewController/Sample/DetailCellNode.m @@ -21,32 +21,33 @@ self = [super init]; if (self == nil) { return self; } - _imageNode = [ASNetworkImageNode new]; + _imageNode = [[ASNetworkImageNode alloc] init]; _imageNode.backgroundColor = ASDisplayNodeDefaultPlaceholderColor(); [self addSubnode:_imageNode]; return self; } - #pragma mark - ASDisplayNode - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize { - ASStaticLayoutSpec *staticSpec = [ASStaticLayoutSpec staticLayoutSpecWithChildren:@[self.imageNode]]; self.imageNode.position = CGPointZero; self.imageNode.sizeRange = ASRelativeSizeRangeMakeWithExactCGSize(constrainedSize.max); - return staticSpec; + return [ASStaticLayoutSpec staticLayoutSpecWithChildren:@[self.imageNode]]; } -- (void)fetchData +- (void)layoutDidFinish { - [super fetchData]; + [super layoutDidFinish]; - [self loadImage]; + // In general set URL of ASNetworkImageNode as soon as possible. Ideally in init or a + // view model setter method. + // In this case as we need to know the size of the node the url is set in layoutDidFinish so + // we have the calculatedSize available + self.imageNode.URL = [self imageURL]; } - #pragma mark - Image - (NSURL *)imageURL @@ -56,9 +57,4 @@ return [NSURL URLWithString:imageURLString]; } -- (void)loadImage -{ - self.imageNode.URL = self.imageURL; -} - @end diff --git a/examples/ASViewController/Sample/DetailRootNode.m b/examples/ASViewController/Sample/DetailRootNode.m index 5aa17f31b5..7dec4b9534 100644 --- a/examples/ASViewController/Sample/DetailRootNode.m +++ b/examples/ASViewController/Sample/DetailRootNode.m @@ -31,38 +31,37 @@ static const NSInteger kImageHeight = 200; if (self == nil) { return self; } _imageCategory = imageCategory; - [self initNode]; - - return self; -} -- (void)initNode -{ // 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.view.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.usesImplicitHierarchyManagement = YES; + + return self; } +- (void)dealloc +{ + _collectionNode.delegate = nil; + _collectionNode.dataSource = nil; +} -#pragma mark - +#pragma mark - ASDisplayNode - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize { - ASStaticLayoutSpec *staticSpec = [ASStaticLayoutSpec staticLayoutSpecWithChildren:@[self.collectionNode]]; self.collectionNode.position = CGPointZero; self.collectionNode.sizeRange = ASRelativeSizeRangeMakeWithExactCGSize(constrainedSize.max); - return staticSpec; + return [ASStaticLayoutSpec staticLayoutSpecWithChildren:@[self.collectionNode]]; } - -#pragma mark - +#pragma mark - ASCollectionDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { @@ -73,7 +72,7 @@ static const NSInteger kImageHeight = 200; { NSString *imageCategory = self.imageCategory; return ^{ - DetailCellNode *node = [DetailCellNode new]; + DetailCellNode *node = [[DetailCellNode alloc] init]; node.row = indexPath.row; node.imageCategory = imageCategory; return node; diff --git a/examples/ASViewController/Sample/DetailViewController.m b/examples/ASViewController/Sample/DetailViewController.m index 78a28fd3a6..9a6577eb0b 100644 --- a/examples/ASViewController/Sample/DetailViewController.m +++ b/examples/ASViewController/Sample/DetailViewController.m @@ -21,7 +21,7 @@ - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator { [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; - [self.node.collectionNode.view invalidateIntrinsicContentSize]; + [self.node.collectionNode.view.collectionViewLayout invalidateLayout]; } @end