Preserve the manual layout code in Kittens example.

This commit is contained in:
Huy Nguyen
2015-07-05 14:22:36 +07:00
parent 33a3412acf
commit daf3daeb51
3 changed files with 58 additions and 7 deletions

View File

@@ -11,6 +11,8 @@
#import <UIKit/UIKit.h>
#define UseAutomaticLayout 1
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;

View File

@@ -10,6 +10,7 @@
*/
#import "BlurbNode.h"
#import "AppDelegate.h"
#import <AsyncDisplayKit/ASDisplayNode+Subclasses.h>
#import <AsyncDisplayKit/ASHighlightOverlayLayer.h>
@@ -72,16 +73,37 @@ static NSString *kLinkAttributeName = @"PlaceKittenNodeLinkAttributeName";
[super didLoad];
}
#if UseAutomaticLayout
- (id<ASLayoutable>)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
return [ASInsetLayoutSpec
newWithInsets:UIEdgeInsetsMake(kTextPadding, kTextPadding, kTextPadding, kTextPadding)
child:
[ASCenterLayoutSpec
newWithCenteringOptions:ASCenterLayoutSpecCenteringX // Center the text horizontally
sizingOptions:ASCenterLayoutSpecSizingOptionMinimumY // Takes up minimum height
child:_textNode]];
return
[ASInsetLayoutSpec
newWithInsets:UIEdgeInsetsMake(kTextPadding, kTextPadding, kTextPadding, kTextPadding)
child:
[ASCenterLayoutSpec
newWithCenteringOptions:ASCenterLayoutSpecCenteringX // Center the text horizontally
sizingOptions:ASCenterLayoutSpecSizingOptionMinimumY // Takes up minimum height
child:_textNode]];
}
#else
- (CGSize)calculateSizeThatFits:(CGSize)constrainedSize
{
// called on a background thread. custom nodes must call -measure: on their subnodes in -calculateSizeThatFits:
CGSize measuredSize = [_textNode measure:CGSizeMake(constrainedSize.width - 2 * kTextPadding,
constrainedSize.height - 2 * kTextPadding)];
return CGSizeMake(constrainedSize.width, measuredSize.height + 2 * kTextPadding);
}
- (void)layout
{
// called on the main thread. we'll use the stashed size from above, instead of blocking on text sizing
CGSize textNodeSize = _textNode.calculatedSize;
_textNode.frame = CGRectMake(roundf((self.calculatedSize.width - textNodeSize.width) / 2.0f),
kTextPadding,
textNodeSize.width,
textNodeSize.height);
}
#endif
#pragma mark -
#pragma mark ASTextNodeDelegate methods.

View File

@@ -10,6 +10,7 @@
*/
#import "KittenNode.h"
#import "AppDelegate.h"
#import <AsyncDisplayKit/ASDisplayNode+Subclasses.h>
@@ -127,6 +128,7 @@ static const CGFloat kInnerPadding = 10.0f;
NSParagraphStyleAttributeName: style };
}
#if UseAutomaticLayout
- (id<ASLayoutable>)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
ASRatioLayoutSpec *imagePlaceholder = [ASRatioLayoutSpec newWithRatio:1.0 child:_imageNode];
@@ -146,12 +148,37 @@ static const CGFloat kInnerPadding = 10.0f;
children:@[imagePlaceholder, _textNode]]];
}
// With box model, you don't need to override this method, unless you want to add custom logic.
- (void)layout
{
[super layout];
// Manually layout the divider.
CGFloat pixelHeight = 1.0f / [[UIScreen mainScreen] scale];
_divider.frame = CGRectMake(0.0f, 0.0f, self.calculatedSize.width, pixelHeight);
}
#else
- (CGSize)calculateSizeThatFits:(CGSize)constrainedSize
{
CGSize imageSize = CGSizeMake(kImageSize, kImageSize);
CGSize textSize = [_textNode measure:CGSizeMake(constrainedSize.width - kImageSize - 2 * kOuterPadding - kInnerPadding,
constrainedSize.height)];
// ensure there's room for the text
CGFloat requiredHeight = MAX(textSize.height, imageSize.height);
return CGSizeMake(constrainedSize.width, requiredHeight + 2 * kOuterPadding);
}
- (void)layout
{
CGFloat pixelHeight = 1.0f / [[UIScreen mainScreen] scale];
_divider.frame = CGRectMake(0.0f, 0.0f, self.calculatedSize.width, pixelHeight);
_imageNode.frame = CGRectMake(kOuterPadding, kOuterPadding, kImageSize, kImageSize);
CGSize textSize = _textNode.calculatedSize;
_textNode.frame = CGRectMake(kOuterPadding + kImageSize + kInnerPadding, kOuterPadding, textSize.width, textSize.height);
}
#endif
@end