Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit 2e384a3

Browse files
committed
Merge pull request #1673 from maicki/AddAutomaticMeasureBeforeLayout
[ASDisplayNode] Add automatic measure before layout
2 parents b8618d3 + 691749d commit 2e384a3

4 files changed

Lines changed: 65 additions & 43 deletions

File tree

AsyncDisplayKit/ASDisplayNode.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#import <AsyncDisplayKit/ASAsciiArtBoxCreator.h>
1616
#import <AsyncDisplayKit/ASLayoutable.h>
1717

18+
#define ASDisplayNodeLoggingEnabled 0
19+
1820
@class ASDisplayNode;
1921

2022
/**

AsyncDisplayKit/ASDisplayNode.mm

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ - (void)_staticInitialize;
4848

4949
@end
5050

51-
//#define LOG(...) NSLog(__VA_ARGS__)
52-
#define LOG(...)
51+
#if ASDisplayNodeLoggingEnabled
52+
#define LOG(...) NSLog(__VA_ARGS__)
53+
#else
54+
#define LOG(...)
55+
#endif
5356

5457
// Conditionally time these scopes to our debug ivars (only exist in debug/profile builds)
5558
#if TIME_DISPLAYNODE_OPS
@@ -1078,19 +1081,54 @@ - (void)__layout
10781081
ASDisplayNodeAssertMainThread();
10791082
ASDN::MutexLocker l(_propertyLock);
10801083
CGRect bounds = self.bounds;
1081-
if (CGRectEqualToRect(bounds, CGRectZero)) {
1082-
// Performing layout on a zero-bounds view often results in frame calculations
1083-
// with negative sizes after applying margins, which will cause
1084-
// measureWithSizeRange: on subnodes to assert.
1084+
1085+
[self measureNodeWithBoundsIfNecessary:bounds];
1086+
1087+
// Performing layout on a zero-bounds view often results in frame calculations
1088+
// with negative sizes after applying margins, which will cause
1089+
// measureWithSizeRange: on subnodes to assert.
1090+
if (!CGRectEqualToRect(bounds, CGRectZero)) {
1091+
_placeholderLayer.frame = bounds;
1092+
[self layout];
1093+
[self layoutDidFinish];
1094+
}
1095+
}
1096+
1097+
- (void)measureNodeWithBoundsIfNecessary:(CGRect)bounds
1098+
{
1099+
// Normally measure will be called before layout occurs. If this doesn't happen, nothing is going to call it at all.
1100+
// We simply call measureWithSizeRange: using a size range equal to whatever bounds were provided to that element or
1101+
// try to measure the node with the largest size as possible
1102+
if (self.supernode == nil && !self.supportsRangeManagedInterfaceState && !_flags.isMeasured) {
1103+
if (CGRectEqualToRect(bounds, CGRectZero)) {
1104+
LOG(@"Warning: No size given for node before node was trying to layout itself: %@. Please provide a frame for the node.", self);
1105+
} else {
1106+
[self measureWithSizeRange:ASSizeRangeMake(CGSizeZero, bounds.size)];
1107+
}
1108+
}
1109+
}
1110+
1111+
- (void)layout
1112+
{
1113+
ASDisplayNodeAssertMainThread();
1114+
1115+
if (!_flags.isMeasured) {
10851116
return;
10861117
}
1087-
_placeholderLayer.frame = bounds;
1088-
[self layout];
1089-
[self layoutDidFinish];
1118+
1119+
[self __layoutSublayouts];
1120+
}
1121+
1122+
- (void)__layoutSublayouts
1123+
{
1124+
for (ASLayout *subnodeLayout in _layout.immediateSublayouts) {
1125+
((ASDisplayNode *)subnodeLayout.layoutableObject).frame = [subnodeLayout frame];
1126+
}
10901127
}
10911128

10921129
- (void)layoutDidFinish
10931130
{
1131+
// Hook for subclasses
10941132
}
10951133

10961134
- (CATransform3D)_transformToAncestor:(ASDisplayNode *)ancestor
@@ -2377,24 +2415,6 @@ - (void)applyLayout:(ASLayout *)layout
23772415
}
23782416
}
23792417

2380-
- (void)layout
2381-
{
2382-
ASDisplayNodeAssertMainThread();
2383-
2384-
if (!_flags.isMeasured) {
2385-
return;
2386-
}
2387-
2388-
[self __layoutSublayouts];
2389-
}
2390-
2391-
- (void)__layoutSublayouts
2392-
{
2393-
for (ASLayout *subnodeLayout in _layout.immediateSublayouts) {
2394-
((ASDisplayNode *)subnodeLayout.layoutableObject).frame = [subnodeLayout frame];
2395-
}
2396-
}
2397-
23982418
- (void)displayWillStart
23992419
{
24002420
ASDisplayNodeAssertMainThread();

examples/Videos/Sample/ViewController.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
99
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1010
*/
11-
#import <AsyncDisplayKit/AsyncDisplayKit.h>
12-
#import <AsyncDisplayKit/ASVideoNode.h>
11+
12+
#include <UIKit/UIKit.h>
1313

1414
@interface ViewController : UIViewController
1515

examples/Videos/Sample/ViewController.m

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
*/
1111

1212
#import "ViewController.h"
13-
#import "ASLayoutSpec.h"
14-
#import "ASStaticLayoutSpec.h"
13+
#import <AsyncDisplayKit/AsyncDisplayKit.h>
1514

1615
@interface ViewController()<ASVideoNodeDelegate>
1716
@property (nonatomic, strong) ASDisplayNode *rootNode;
@@ -22,12 +21,23 @@ @implementation ViewController
2221

2322
#pragma mark - UIViewController
2423

25-
- (void)viewWillAppear:(BOOL)animated
24+
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
2625
{
27-
[super viewWillAppear:animated];
26+
self = [super initWithNibName:nil bundle:nil];
27+
if (self) {
2828

29+
30+
}
31+
return self;
32+
}
33+
34+
- (void)viewDidLoad
35+
{
36+
[super viewDidLoad];
37+
2938
// Root node for the view controller
3039
_rootNode = [ASDisplayNode new];
40+
_rootNode.frame = self.view.bounds;
3141
_rootNode.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
3242

3343
ASVideoNode *guitarVideoNode = self.guitarVideoNode;
@@ -54,16 +64,6 @@ - (void)viewWillAppear:(BOOL)animated
5464
[self.view addSubnode:_rootNode];
5565
}
5666

57-
- (void)viewDidLayoutSubviews
58-
{
59-
[super viewDidLayoutSubviews];
60-
61-
// After all subviews are layed out we have to measure it and move the root node to the right place
62-
CGSize viewSize = self.view.bounds.size;
63-
[self.rootNode measureWithSizeRange:ASSizeRangeMake(viewSize, viewSize)];
64-
[self.rootNode setNeedsLayout];
65-
}
66-
6767
#pragma mark - Getter / Setter
6868

6969
- (ASVideoNode *)guitarVideoNode;

0 commit comments

Comments
 (0)