Improve ASViewController example

This commit is contained in:
Michael Schneider 2016-03-16 09:36:35 -07:00
parent 192e9398e5
commit 788cdbd326
5 changed files with 23 additions and 51 deletions

View File

@ -1,3 +1,3 @@
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
platform :ios, '7.1'
pod 'AsyncDisplayKit', :path => '../..'

View File

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

View File

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

View File

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

View File

@ -21,7 +21,7 @@
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[self.node.collectionNode.view invalidateIntrinsicContentSize];
[self.node.collectionNode.view.collectionViewLayout invalidateLayout];
}
@end