diff --git a/AsyncDisplayKit/ASLayoutSpec+Debug.m b/AsyncDisplayKit/ASLayoutSpec+Debug.m index 2817b274..44d8cd12 100644 --- a/AsyncDisplayKit/ASLayoutSpec+Debug.m +++ b/AsyncDisplayKit/ASLayoutSpec+Debug.m @@ -17,8 +17,8 @@ { self = [super init]; if (self) { - self.layer.borderWidth = 2; - self.layer.borderColor = [[UIColor redColor] CGColor]; + self.borderWidth = 2; + self.borderColor = [[UIColor redColor] CGColor]; self.layoutSpec = layoutSpec; self.layoutSpec.neverShouldVisualize = YES; self.usesImplicitHierarchyManagement = YES; @@ -52,10 +52,10 @@ _layoutSpec = layoutSpec; // FIXME: should copy layoutSpec properities to self? if ([layoutSpec isKindOfClass:[ASInsetLayoutSpec class]]) { - self.layer.borderColor = [[UIColor redColor] CGColor]; + self.borderColor = [[UIColor redColor] CGColor]; } else if ([layoutSpec isKindOfClass:[ASStackLayoutSpec class]]) { - self.layer.borderColor = [[UIColor greenColor] CGColor]; + self.borderColor = [[UIColor greenColor] CGColor]; } } diff --git a/AsyncDisplayKit/ASLayoutableInspectorCell.m b/AsyncDisplayKit/ASLayoutableInspectorCell.m index 3e70595f..6a18351b 100644 --- a/AsyncDisplayKit/ASLayoutableInspectorCell.m +++ b/AsyncDisplayKit/ASLayoutableInspectorCell.m @@ -61,6 +61,8 @@ __weak static ASLayoutableInspectorCell *__currentlyOpenedCell = nil; _textNode = [[ASTextNode alloc] init]; _textNode.attributedString = [ASLayoutableInspectorCell propertyValueAttributedStringForProperty:property withLayoutable:layoutable]; + //_buttonNode.select = method here + _textNode2 = [[ASTextNode alloc] init]; _textNode2.attributedString = [ASLayoutableInspectorCell propertyValueDetailAttributedStringForProperty:property withLayoutable:layoutable]; diff --git a/examples/ASLayoutSpecPlayground/Sample/PlaygroundContainerNode.h b/examples/ASLayoutSpecPlayground/Sample/PlaygroundContainerNode.h index ff6465d9..01d8eb67 100644 --- a/examples/ASLayoutSpecPlayground/Sample/PlaygroundContainerNode.h +++ b/examples/ASLayoutSpecPlayground/Sample/PlaygroundContainerNode.h @@ -14,8 +14,10 @@ @end -@interface PlaygroundContainerNode : ASDisplayNode +@interface PlaygroundContainerNode : ASCellNode @property (nonatomic, weak) id delegate; +- (instancetype)initWithIndex:(NSUInteger)index; + @end diff --git a/examples/ASLayoutSpecPlayground/Sample/PlaygroundContainerNode.m b/examples/ASLayoutSpecPlayground/Sample/PlaygroundContainerNode.m index 42734e80..74916746 100644 --- a/examples/ASLayoutSpecPlayground/Sample/PlaygroundContainerNode.m +++ b/examples/ASLayoutSpecPlayground/Sample/PlaygroundContainerNode.m @@ -22,7 +22,7 @@ #pragma mark - Lifecycle -- (instancetype)init +- (instancetype)initWithIndex:(NSUInteger)index { self = [super init]; @@ -30,7 +30,7 @@ self.backgroundColor = [UIColor colorWithRed:255/255.0 green:181/255.0 blue:68/255.0 alpha:1]; self.usesImplicitHierarchyManagement = YES; - _playgroundNode = [[PlaygroundNode alloc] init]; + _playgroundNode = [[PlaygroundNode alloc] initWithIndex:index]; _resizeHandle = [[ASImageNode alloc] init]; _resizeHandle.image = [UIImage imageNamed:@"resizeHandle"]; diff --git a/examples/ASLayoutSpecPlayground/Sample/PlaygroundNode.h b/examples/ASLayoutSpecPlayground/Sample/PlaygroundNode.h index be577502..c15b0ea6 100644 --- a/examples/ASLayoutSpecPlayground/Sample/PlaygroundNode.h +++ b/examples/ASLayoutSpecPlayground/Sample/PlaygroundNode.h @@ -10,4 +10,6 @@ @interface PlaygroundNode : ASDisplayNode +- (instancetype)initWithIndex:(NSUInteger)index; + @end diff --git a/examples/ASLayoutSpecPlayground/Sample/PlaygroundNode.m b/examples/ASLayoutSpecPlayground/Sample/PlaygroundNode.m index c62004ac..00050fca 100644 --- a/examples/ASLayoutSpecPlayground/Sample/PlaygroundNode.m +++ b/examples/ASLayoutSpecPlayground/Sample/PlaygroundNode.m @@ -17,6 +17,7 @@ @implementation PlaygroundNode { + NSUInteger _index; ASNetworkImageNode *_userAvatarImageView; ASNetworkImageNode *_photoImageView; ASTextNode *_userNameLabel; @@ -28,7 +29,7 @@ #pragma mark - Lifecycle -- (instancetype)init +- (instancetype)initWithIndex:(NSUInteger)index { self = [super init]; @@ -39,6 +40,8 @@ self.shouldVisualizeLayoutSpecs = YES; self.shouldCacheLayoutSpec = YES; + _index = index; + _userAvatarImageView = [[ASNetworkImageNode alloc] init]; _userAvatarImageView.URL = [NSURL URLWithString:@"https://s-media-cache-ak0.pinimg.com/avatars/503h_1458880322_140.jpg"]; @@ -74,6 +77,66 @@ } - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize +{ + switch (_index) { + case 1: + return [self layoutSpecThatFitsNavBar:constrainedSize]; + case 2: + return [self layoutSpecThatFitsASDKgram:constrainedSize]; + default: + return [self layoutSpecThatFitsASDKgram:constrainedSize]; + break; + } +} + +- (ASLayoutSpec *)layoutSpecThatFitsNavBar:(ASSizeRange)constrainedSize +{ + // username / photo location header vertical stack + + _userNameLabel.flexShrink = YES; + _photoLocationLabel.flexShrink = YES; + + ASStackLayoutSpec *headerSubStack = [ASStackLayoutSpec verticalStackLayoutSpec]; + headerSubStack.flexShrink = YES; + + if (_photoLocationLabel.attributedString) { + [headerSubStack setChildren:@[_userNameLabel, _photoLocationLabel]]; + } else { + [headerSubStack setChildren:@[_userNameLabel]]; + } + + // header stack + + _userAvatarImageView.preferredFrameSize = CGSizeMake(USER_IMAGE_HEIGHT, USER_IMAGE_HEIGHT); + _photoTimeIntervalSincePostLabel.spacingBefore = HORIZONTAL_BUFFER; // hack to remove double spaces around spacer + + UIEdgeInsets avatarInsets = UIEdgeInsetsMake(HORIZONTAL_BUFFER, 0, HORIZONTAL_BUFFER, HORIZONTAL_BUFFER); + ASInsetLayoutSpec *avatarInset = [ASInsetLayoutSpec insetLayoutSpecWithInsets:avatarInsets child:_userAvatarImageView]; + + ASLayoutSpec *spacer = [[ASLayoutSpec alloc] init]; + spacer.flexGrow = YES; + spacer.flexShrink = YES; // FIXME: this overrides stuff :) THIS IS A SYSTEMIC ISSUE - can we make layoutSpecThatFits only run once? cache layoutSpec, just use new constrainedSize, don't put properties in layoutSpecThatFits + // separate the idea of laying out and rerunning with new constrainedSize + + ASStackLayoutSpec *headerStack = [ASStackLayoutSpec horizontalStackLayoutSpec]; + headerStack.alignItems = ASStackLayoutAlignItemsCenter; // center items vertically in horizontal stack + headerStack.justifyContent = ASStackLayoutJustifyContentStart; // justify content to the left side of the header stack + headerStack.flexShrink = YES; + headerStack.flexGrow = YES; + + [headerStack setChildren:@[avatarInset, headerSubStack, spacer, _photoTimeIntervalSincePostLabel]]; + + // header inset stack + + UIEdgeInsets insets = UIEdgeInsetsMake(0, HORIZONTAL_BUFFER, 0, HORIZONTAL_BUFFER); + ASInsetLayoutSpec *headerWithInset = [ASInsetLayoutSpec insetLayoutSpecWithInsets:insets child:headerStack]; + headerWithInset.flexShrink = YES; + headerWithInset.flexGrow = YES; + + return headerWithInset; +} + +- (ASLayoutSpec *)layoutSpecThatFitsASDKgram:(ASSizeRange)constrainedSize { // username / photo location header vertical stack diff --git a/examples/ASLayoutSpecPlayground/Sample/ViewController.m b/examples/ASLayoutSpecPlayground/Sample/ViewController.m index 3a915d52..607a803b 100644 --- a/examples/ASLayoutSpecPlayground/Sample/ViewController.m +++ b/examples/ASLayoutSpecPlayground/Sample/ViewController.m @@ -10,11 +10,12 @@ #import "PlaygroundContainerNode.h" #import "ASLayoutableInspectorNode.h" -@interface ViewController () +@interface ViewController () @end @implementation ViewController { + ASPagerNode *_pagerNode; ASSizeRange _sizeRange; } @@ -22,19 +23,35 @@ - (instancetype)init { - PlaygroundContainerNode *containerNode = [[PlaygroundContainerNode alloc] init]; - self = [super initWithNode:containerNode]; + _pagerNode = [[ASPagerNode alloc] init]; + self = [super initWithNode:_pagerNode]; if (self) { + _pagerNode.dataSource = self; self.navigationItem.title = @"ASLayoutSpec Playground"; self.edgesForExtendedLayout = UIRectEdgeNone; - containerNode.delegate = self; [ASLayoutableInspectorNode sharedInstance].delegate = self; } return self; } +#pragma mark - ASPagerNodeDataSource + +- (NSInteger)numberOfPagesInPagerNode:(ASPagerNode *)pagerNode +{ + return 2; +} + +- (ASCellNodeBlock)pagerNode:(ASPagerNode *)pagerNode nodeBlockAtIndex:(NSInteger)index +{ + return ^{ + PlaygroundContainerNode *containerCellNode = [[PlaygroundContainerNode alloc] initWithIndex:index]; + containerCellNode.delegate = self; + return containerCellNode; + }; +} + // [ASViewController] Override this method to provide a custom size range to the backing node. // Neccessary to allow the user to stretch / shrink the size of playground container. - (ASSizeRange)nodeConstrainedSize @@ -45,12 +62,22 @@ return _sizeRange; } +- (ASSizeRange)pagerNode:(ASPagerNode *)pagerNode constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath +{ + if (CGSizeEqualToSize(_sizeRange.max, CGSizeZero)) { + return [super nodeConstrainedSize]; + } + return _sizeRange; +} + #pragma mark - PlaygroundContainerNodeDelegate - (void)relayoutWithSize:(ASSizeRange)size { +// NSLog(@"DELEGATE constrainedSize = %@", NSStringFromCGSize(size.max)); _sizeRange = size; [self.view setNeedsLayout]; + [_pagerNode reloadData]; } #pragma mark - ASLayoutableInspectorNodeDelegate