diff --git a/AsyncDisplayKit/ASDisplayNode.h b/AsyncDisplayKit/ASDisplayNode.h index c0361d0ce6..47e03d09a7 100644 --- a/AsyncDisplayKit/ASDisplayNode.h +++ b/AsyncDisplayKit/ASDisplayNode.h @@ -17,6 +17,8 @@ #import #import +#define ASDisplayNodeLoggingEnabled 0 + @class ASDisplayNode; /** diff --git a/AsyncDisplayKit/ASDisplayNode.mm b/AsyncDisplayKit/ASDisplayNode.mm index 81c02b9dfc..98038f5894 100644 --- a/AsyncDisplayKit/ASDisplayNode.mm +++ b/AsyncDisplayKit/ASDisplayNode.mm @@ -53,8 +53,11 @@ NSString * const ASRenderingEngineDidDisplayNodesScheduledBeforeTimestamp = @"AS @end -//#define LOG(...) NSLog(__VA_ARGS__) -#define LOG(...) +#if ASDisplayNodeLoggingEnabled + #define LOG(...) NSLog(__VA_ARGS__) +#else + #define LOG(...) +#endif // Conditionally time these scopes to our debug ivars (only exist in debug/profile builds) #if TIME_DISPLAYNODE_OPS @@ -880,7 +883,7 @@ static ASDisplayNodeMethodOverrides GetASDisplayNodeMethodOverrides(Class c) - (void)animateLayoutTransition:(id)context { - [self __layoutSublayouts]; + [self __layoutSubnodes]; [context completeTransition:YES]; } @@ -1114,26 +1117,61 @@ static ASDisplayNodeMethodOverrides GetASDisplayNodeMethodOverrides(Class c) ASDisplayNodeAssertMainThread(); ASDN::MutexLocker l(_propertyLock); CGRect bounds = self.bounds; + + [self measureNodeWithBoundsIfNecessary:bounds]; + if (CGRectEqualToRect(bounds, CGRectZero)) { // Performing layout on a zero-bounds view often results in frame calculations // with negative sizes after applying margins, which will cause // measureWithSizeRange: on subnodes to assert. return; } - + // Handle placeholder layer creation in case the size of the node changed after the initial placeholder layer // was created if ([self _shouldHavePlaceholderLayer]) { [self _setupPlaceholderLayerIfNeeded]; } _placeholderLayer.frame = bounds; - + [self layout]; [self layoutDidFinish]; } +- (void)measureNodeWithBoundsIfNecessary:(CGRect)bounds +{ + // Normally measure will be called before layout occurs. If this doesn't happen, nothing is going to call it at all. + // We simply call measureWithSizeRange: using a size range equal to whatever bounds were provided to that element + if (self.supernode == nil && !self.supportsRangeManagedInterfaceState && [self _hasDirtyLayout]) { + if (CGRectEqualToRect(bounds, CGRectZero)) { + LOG(@"Warning: No size given for node before node was trying to layout itself: %@. Please provide a frame for the node.", self); + } else { + [self measureWithSizeRange:ASSizeRangeMake(bounds.size, bounds.size)]; + } + } +} + +- (void)layout +{ + ASDisplayNodeAssertMainThread(); + + if ([self _hasDirtyLayout]) { + return; + } + + [self __layoutSubnodes]; +} + +- (void)__layoutSubnodes +{ + for (ASLayout *subnodeLayout in _layout.sublayouts) { + ((ASDisplayNode *)subnodeLayout.layoutableObject).frame = [subnodeLayout frame]; + } +} + - (void)layoutDidFinish { + // Hook for subclasses } - (CATransform3D)_transformToAncestor:(ASDisplayNode *)ancestor @@ -2463,24 +2501,6 @@ void recursivelyTriggerDisplayForLayer(CALayer *layer, BOOL shouldBlock) [layoutTransition startTransition]; } -- (void)layout -{ - ASDisplayNodeAssertMainThread(); - - if ([self _hasDirtyLayout]) { - return; - } - - [self __layoutSublayouts]; -} - -- (void)__layoutSublayouts -{ - for (ASLayout *subnodeLayout in _layout.sublayouts) { - ((ASDisplayNode *)subnodeLayout.layoutableObject).frame = [subnodeLayout frame]; - } -} - - (void)displayWillStart { ASDisplayNodeAssertMainThread(); diff --git a/examples/Videos/Sample/ViewController.h b/examples/Videos/Sample/ViewController.h index 7cce1c400a..2b35e7f47d 100644 --- a/examples/Videos/Sample/ViewController.h +++ b/examples/Videos/Sample/ViewController.h @@ -14,8 +14,7 @@ // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -#import -#import +#import @interface ViewController : UIViewController diff --git a/examples/Videos/Sample/ViewController.m b/examples/Videos/Sample/ViewController.m index e2a6e22b88..1b390386a2 100644 --- a/examples/Videos/Sample/ViewController.m +++ b/examples/Videos/Sample/ViewController.m @@ -16,8 +16,7 @@ // #import "ViewController.h" -#import "ASLayoutSpec.h" -#import "ASStaticLayoutSpec.h" +#import @interface ViewController() @property (nonatomic, strong) ASDisplayNode *rootNode; @@ -28,12 +27,13 @@ #pragma mark - UIViewController -- (void)viewWillAppear:(BOOL)animated +- (void)viewDidLoad { - [super viewWillAppear:animated]; - + [super viewDidLoad]; + // Root node for the view controller _rootNode = [ASDisplayNode new]; + _rootNode.frame = self.view.bounds; _rootNode.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; ASVideoNode *guitarVideoNode = self.guitarVideoNode; @@ -68,16 +68,6 @@ [self.view addSubnode:_rootNode]; } -- (void)viewDidLayoutSubviews -{ - [super viewDidLayoutSubviews]; - - // After all subviews are layed out we have to measure it and move the root node to the right place - CGSize viewSize = self.view.bounds.size; - [self.rootNode measureWithSizeRange:ASSizeRangeMake(viewSize, viewSize)]; - [self.rootNode setNeedsLayout]; -} - #pragma mark - Getter / Setter - (ASVideoNode *)guitarVideoNode;