Some more stuff in flux

This commit is contained in:
Michael Schneider 2016-11-10 21:10:03 -08:00
parent c61ba65ba3
commit 6f40e9a654
4 changed files with 83 additions and 16 deletions

View File

@ -716,10 +716,20 @@ static ASDisplayNodeMethodOverrides GetASDisplayNodeMethodOverrides(Class c)
#pragma clang diagnostic pop
}
- (ASLayout *)cacheLayoutThatFits:(ASSizeRange)constrainedSize
{
return [self cacheLayoutThatFits:constrainedSize parentSize:constrainedSize.max];
}
- (ASLayout *)layoutThatFits:(ASSizeRange)constrainedSize parentSize:(CGSize)parentSize
{
ASDN::MutexLocker l(__instanceLock__);
return [self calculateLayoutThatFits:constrainedSize restrictedToSize:self.style.size relativeToParentSize:parentSize];
}
- (ASLayout *)cacheLayoutThatFits:(ASSizeRange)constrainedSize parentSize:(CGSize)parentSize
{
if ([self shouldCalculateLayoutWithConstrainedSize:constrainedSize parentSize:parentSize] == NO) {
ASDisplayNodeAssertNotNil(_calculatedDisplayNodeLayout->layout, @"-[ASDisplayNode layoutThatFits:parentSize:] _layout should not be nil! %@", self);
return _calculatedDisplayNodeLayout->layout ? : [ASLayout layoutWithLayoutElement:self size:{0, 0}];
@ -1504,7 +1514,7 @@ static ASDisplayNodeMethodOverrides GetASDisplayNodeMethodOverrides(Class c)
LOG(@"Warning: No size given for node before node was trying to layout itself: %@. Please provide a frame for the node.", self);
} else {
if (CGSizeEqualToSize(calculatedLayoutSize, bounds.size) == NO) {
[self layoutThatFits:ASSizeRangeMake(bounds.size)];
[self cacheLayoutThatFits:ASSizeRangeMake(bounds.size)];
}
}
}

View File

@ -49,12 +49,17 @@
[self.view addSubnode:self.sizingNode];
}
#pragma mark - Rotation
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[self.node.collectionNode.view.collectionViewLayout invalidateLayout];
[super viewWillAppear:animated];
// Initial size of sizing node
self.sizingNode.frame = CGRectMake(100, 100, 50, 50);
// Start some timer to chang ethe size randomly
[NSTimer scheduledTimerWithTimeInterval:2.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
//[self updateNodeLayoutRandom];
}];
}
- (void)viewDidLayoutSubviews
@ -66,23 +71,61 @@
#pragma mark - Update the node based on the new size
// The sizing delegate will get callbacks if the size did invalidate of the display node. It's the job of the delegate
// to get the new size from the display node and update the frame based on the returned size
- (void)displayNodeDidInvalidateSize:(ASDisplayNode *)displayNode
{
// ASDisplayNodeSizingDelegate / ASDisplayNodeSizingHandlers
[self updateNodeLayout];
/*dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self updateNodeLayoutRandom];
});*/
/*[NSTimer scheduledTimerWithTimeInterval:2.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
[self updateNodeLayoutRandom];
}];*/
}
- (void)updateNodeLayout
{
// Adjust the layout on the new layout
//return;
// Use the bounds of the view and get the fitting size
// This does not have any side effects, but can be called on the main thread without any problems
CGSize size = [self.sizingNode sizeThatFits:CGSizeMake(CGFLOAT_MAX, 100.0)];
size.width -= 10;
//size.width -= 10;
//[self.sizingNode setNeedsLayout];
self.sizingNode.frame = CGRectMake((self.view.bounds.size.width - size.width) / 2.0,
(self.view.bounds.size.height - size.height) / 2.0,
size.width, size.height);
//dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.sizingNode.frame = CGRectInset(self.sizingNode.frame, 10, 10);
//});
}
- (void)updateNodeLayoutRandom
{
// Pick a randome width and height and set the frame of the node
CGSize size = CGSizeZero;
size.width = arc4random_uniform(self.view.bounds.size.width);
size.height = arc4random_uniform(self.view.bounds.size.height);
//[self.sizingNode setNeedsLayout];
self.sizingNode.frame = CGRectMake((self.view.bounds.size.width - size.width) / 2.0,
(self.view.bounds.size.height - size.height) / 2.0,
size.width, size.height);
}
#pragma mark - Rotation
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[self.node.collectionNode.view.collectionViewLayout invalidateLayout];
}
@end

View File

@ -8,6 +8,7 @@
#import <AsyncDisplayKit/AsyncDisplayKit.h>
// ASDisplayNodeSizingDelegate / ASDisplayNodeSizingHandlers
@interface ASDisplayNodeSizingDelegate : NSObject
- (void)displayNodeDidInvalidateSize:(ASDisplayNode *)displayNode;
@end

View File

@ -24,12 +24,19 @@
self.automaticallyManagesSubnodes = YES;
//_subnode = [ASDisplayNode new];
//_subnode.backgroundColor = [UIColor redColor];
self.backgroundColor = [UIColor greenColor];
_textNode = [ASTextNode new];
_textNode.backgroundColor = [UIColor blueColor];
_textNode.autoresizingMask = UIViewAutoresizingNone;
//_textNode.autoresizingMask = UIViewAutoresizingNone;
_subnode = [ASDisplayNode new];
_subnode.backgroundColor = [UIColor redColor];
_subnode.automaticallyManagesSubnodes = YES;
_subnode.layoutSpecBlock = ^ASLayoutSpec *(__kindof ASDisplayNode * _Nonnull node, ASSizeRange constrainedSize) {
UIEdgeInsets insets = UIEdgeInsetsMake(10, 10, 10, 10);
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:insets child:_textNode];
};
_state = 0;
}
@ -61,7 +68,8 @@
self.textNode.attributedText = [[NSAttributedString alloc] initWithString:text];
// Invalidate the layout for now and bubble it up until the root node to let the size provider know that
// that a size change happened
// that a size change could have happened
// --> Do we even need to invalidate the layout?
[self setNeedsLayout];
// If someone calls `setNeedsLayout` we have to inform the sizing delegate of the root node to be able
@ -77,9 +85,14 @@
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
// Layout description based on state
//self.subnode.style.preferredSize = constrainedSize.max;
UIEdgeInsets insets = UIEdgeInsetsMake(10, 10, 10, 10);
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:insets child:_textNode];
// UIEdgeInsets insets = UIEdgeInsetsMake(10, 10, 10, 10);
// return [ASInsetLayoutSpec insetLayoutSpecWithInsets:insets child:_textNode];
//return [ASWrapperLayoutSpec wrapperWithLayoutElement:self.subnode];
return [ASCenterLayoutSpec
centerLayoutSpecWithCenteringOptions:ASCenterLayoutSpecCenteringXY
sizingOptions:ASCenterLayoutSpecSizingOptionDefault
child:self.subnode];
}