mirror of
https://github.com/HackPlan/AsyncDisplayKit.git
synced 2026-04-23 03:20:39 +08:00
add pager ndoe
This commit is contained in:
@@ -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];
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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];
|
||||
|
||||
|
||||
@@ -14,8 +14,10 @@
|
||||
|
||||
@end
|
||||
|
||||
@interface PlaygroundContainerNode : ASDisplayNode
|
||||
@interface PlaygroundContainerNode : ASCellNode
|
||||
|
||||
@property (nonatomic, weak) id<PlaygroundContainerNodeDelegate> delegate;
|
||||
|
||||
- (instancetype)initWithIndex:(NSUInteger)index;
|
||||
|
||||
@end
|
||||
|
||||
@@ -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"];
|
||||
|
||||
@@ -10,4 +10,6 @@
|
||||
|
||||
@interface PlaygroundNode : ASDisplayNode
|
||||
|
||||
- (instancetype)initWithIndex:(NSUInteger)index;
|
||||
|
||||
@end
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -10,11 +10,12 @@
|
||||
#import "PlaygroundContainerNode.h"
|
||||
#import "ASLayoutableInspectorNode.h"
|
||||
|
||||
@interface ViewController () <PlaygroundContainerNodeDelegate, ASLayoutableInspectorNodeDelegate>
|
||||
@interface ViewController () <ASPagerNodeDataSource, ASLayoutableInspectorNodeDelegate, PlaygroundContainerNodeDelegate>
|
||||
@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
|
||||
|
||||
Reference in New Issue
Block a user