From b17ff48c59b6e0c0ad0357f6c5444eacab2ba4a3 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Wed, 8 Apr 2020 11:57:38 +0800 Subject: [PATCH] iOS:refact requestLayout logic --- .../Pod/Classes/Plugin/DoricShaderPlugin.m | 2 + .../Classes/Refresh/DoricRefreshableNode.m | 6 +-- .../Classes/Refresh/DoricSwipeRefreshLayout.m | 39 +++---------------- doric-iOS/Pod/Classes/Shader/DoricGroupNode.m | 5 +++ doric-iOS/Pod/Classes/Shader/DoricLayouts.h | 2 + doric-iOS/Pod/Classes/Shader/DoricLayouts.m | 1 - doric-iOS/Pod/Classes/Shader/DoricListNode.m | 1 + doric-iOS/Pod/Classes/Shader/DoricRootNode.m | 1 + .../Pod/Classes/Shader/DoricScrollerNode.m | 8 +++- doric-iOS/Pod/Classes/Shader/DoricSuperNode.m | 4 -- doric-iOS/Pod/Classes/Shader/DoricViewNode.m | 4 +- 11 files changed, 27 insertions(+), 46 deletions(-) diff --git a/doric-iOS/Pod/Classes/Plugin/DoricShaderPlugin.m b/doric-iOS/Pod/Classes/Plugin/DoricShaderPlugin.m index c7f17be8..ecbd5d78 100644 --- a/doric-iOS/Pod/Classes/Plugin/DoricShaderPlugin.m +++ b/doric-iOS/Pod/Classes/Plugin/DoricShaderPlugin.m @@ -41,9 +41,11 @@ - (void)render:(NSDictionary *)argument withPromise:(DoricPromise *)promise { if (self.doricContext.rootNode.viewId == nil) { self.doricContext.rootNode.viewId = viewId; [self.doricContext.rootNode blend:argument[@"props"]]; + [self.doricContext.rootNode requestLayout]; } else { DoricViewNode *viewNode = [self.doricContext targetViewNode:viewId]; [viewNode blend:argument[@"props"]]; + [viewNode requestLayout]; } [promise resolve:nil]; }); diff --git a/doric-iOS/Pod/Classes/Refresh/DoricRefreshableNode.m b/doric-iOS/Pod/Classes/Refresh/DoricRefreshableNode.m index 282fb4fc..c49a12c6 100644 --- a/doric-iOS/Pod/Classes/Refresh/DoricRefreshableNode.m +++ b/doric-iOS/Pod/Classes/Refresh/DoricRefreshableNode.m @@ -31,7 +31,6 @@ @implementation DoricRefreshableNode - (DoricSwipeRefreshLayout *)build { return [[DoricSwipeRefreshLayout new] also:^(DoricSwipeRefreshLayout *it) { it.swipePullingDelegate = self; - }]; } @@ -62,8 +61,7 @@ - (DoricViewNode *)subNodeWithViewId:(NSString *)viewId { } } -- (void)blend:(NSDictionary *)props { - [super blend:props]; +- (void)afterBlended:(NSDictionary *)props { [self blendContent]; [self blendHeader]; } @@ -102,6 +100,7 @@ - (void)blendContent { self.view.contentView = it.view; }]; } + [self.view.contentView.doricLayout apply:self.view.frame.size]; } - (void)blendHeader { @@ -138,6 +137,7 @@ - (void)blendHeader { self.view.headerView = it.view; }]; } + [self.view.headerView.doricLayout apply:self.view.frame.size]; } - (void)blendSubNode:(NSDictionary *)subModel { diff --git a/doric-iOS/Pod/Classes/Refresh/DoricSwipeRefreshLayout.m b/doric-iOS/Pod/Classes/Refresh/DoricSwipeRefreshLayout.m index 610867c1..c0bf7d61 100644 --- a/doric-iOS/Pod/Classes/Refresh/DoricSwipeRefreshLayout.m +++ b/doric-iOS/Pod/Classes/Refresh/DoricSwipeRefreshLayout.m @@ -20,7 +20,6 @@ #import "DoricSwipeRefreshLayout.h" #import "UIView+Doric.h" #import "DoricLayouts.h" -#import "Doric.h" @interface DoricSwipeRefreshLayout () @@ -54,41 +53,13 @@ - (instancetype)init { return self; } -//- (CGSize)sizeThatFits:(CGSize)size { -// if (self.contentView) { -// return [self.contentView measureSize:size]; -// } -// return CGSizeZero; -//} -// -//- (BOOL)requestFromSubview:(UIView *)subview { -// if (subview == self.headerView) { -// return NO; -// } -// return [super requestFromSubview:subview]; -//} - -- (void)layoutSubviews { - [super layoutSubviews]; +- (CGSize)sizeThatFits:(CGSize)size { + if (self.contentView) { + return self.contentView.frame.size; + } + return CGSizeZero; } -//- (void)layoutSelf:(CGSize)targetSize { -// if (self.contentOffset.y != 0) { -// return; -// } -// self.width = targetSize.width; -// self.height = targetSize.height; -// [self.headerView also:^(UIView *it) { -// [it layoutSelf:[it measureSize:targetSize]]; -// it.bottom = 0; -// it.centerX = self.centerX; -// }]; -// [self.contentView also:^(UIView *it) { -// [it layoutSelf:targetSize]; -// }]; -// self.contentSize = self.frame.size; -//} - - (void)setContentView:(UIView *)contentView { if (_contentView) { [_contentView removeFromSuperview]; diff --git a/doric-iOS/Pod/Classes/Shader/DoricGroupNode.m b/doric-iOS/Pod/Classes/Shader/DoricGroupNode.m index 9fa56198..c5972287 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricGroupNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricGroupNode.m @@ -161,4 +161,9 @@ - (DoricViewNode *)subNodeWithViewId:(NSString *)viewId { return nil; } +- (void)requestLayout { + for (DoricViewNode *node in self.childNodes) { + [node requestLayout]; + } +} @end diff --git a/doric-iOS/Pod/Classes/Shader/DoricLayouts.h b/doric-iOS/Pod/Classes/Shader/DoricLayouts.h index aa6b4ac1..0cffe37a 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricLayouts.h +++ b/doric-iOS/Pod/Classes/Shader/DoricLayouts.h @@ -92,6 +92,8 @@ typedef NS_ENUM(NSInteger, DoricGravity) { @property(nonatomic, assign) CGFloat maxHeight; +@property(nonatomic, assign) BOOL resolved; + - (instancetype)init; - (void)apply; diff --git a/doric-iOS/Pod/Classes/Shader/DoricLayouts.m b/doric-iOS/Pod/Classes/Shader/DoricLayouts.m index f56b1dad..6ed67e58 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricLayouts.m +++ b/doric-iOS/Pod/Classes/Shader/DoricLayouts.m @@ -55,7 +55,6 @@ @interface DoricLayout () @property(nonatomic, assign) CGFloat measuredHeight; @property(nonatomic, assign) CGFloat measuredX; @property(nonatomic, assign) CGFloat measuredY; -@property(nonatomic, assign) BOOL resolved; @end @implementation DoricLayout diff --git a/doric-iOS/Pod/Classes/Shader/DoricListNode.m b/doric-iOS/Pod/Classes/Shader/DoricListNode.m index 9f9c6d77..119d0271 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricListNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricListNode.m @@ -141,6 +141,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N DoricListItemNode *node = cell.doricListItemNode; node.viewId = model[@"id"]; [node blend:props]; + [node requestLayout]; [self callItem:position height:node.view.height]; return cell; } diff --git a/doric-iOS/Pod/Classes/Shader/DoricRootNode.m b/doric-iOS/Pod/Classes/Shader/DoricRootNode.m index 17b434f9..960ab886 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricRootNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricRootNode.m @@ -61,5 +61,6 @@ - (void)setupRootView:(UIView *)view { - (void)requestLayout { [self.view.doricLayout apply]; + [super requestLayout]; } @end diff --git a/doric-iOS/Pod/Classes/Shader/DoricScrollerNode.m b/doric-iOS/Pod/Classes/Shader/DoricScrollerNode.m index b8197b72..e80e280b 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricScrollerNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricScrollerNode.m @@ -37,7 +37,9 @@ - (void)setContentView:(UIView *)contentView { - (CGSize)sizeThatFits:(CGSize)size { if (self.contentView) { - [self.contentView.doricLayout apply:self.frame.size]; + if (!self.contentView.doricLayout.resolved) { + [self.contentView.doricLayout apply:self.frame.size]; + } return self.contentView.frame.size; } return CGSizeZero; @@ -117,6 +119,10 @@ - (void)afterBlended:(NSDictionary *)props { } } +- (void)requestLayout { + [self.view.contentView.doricLayout apply:self.view.frame.size]; +} + - (void)blendView:(DoricScrollView *)view forPropName:(NSString *)name propValue:(id)prop { if ([@"content" isEqualToString:name]) { self.childViewId = prop; diff --git a/doric-iOS/Pod/Classes/Shader/DoricSuperNode.m b/doric-iOS/Pod/Classes/Shader/DoricSuperNode.m index 7fd62944..7b80268b 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricSuperNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricSuperNode.m @@ -127,8 +127,4 @@ - (DoricViewNode *)subNodeWithViewId:(NSString *)viewId { NSStringFromSelector(_cmd)); return nil; } - -- (void)requestLayout { - [self.view setNeedsLayout]; -} @end diff --git a/doric-iOS/Pod/Classes/Shader/DoricViewNode.m b/doric-iOS/Pod/Classes/Shader/DoricViewNode.m index ba16c72d..a856281f 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricViewNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricViewNode.m @@ -139,8 +139,7 @@ - (void)blend:(NSDictionary *)props { } [self afterBlended:props]; [self transformProperties]; - [self requestLayout]; - [self.gradientLayer also:^(CAGradientLayer* it) { + [self.gradientLayer also:^(CAGradientLayer *it) { dispatch_async(dispatch_get_main_queue(), ^{ it.frame = CGRectMake(0, 0, self.view.width, self.view.height); }); @@ -349,7 +348,6 @@ + (__kindof DoricViewNode *)create:(DoricContext *)context withType:(NSString *) } - (void)requestLayout { - [self.superNode requestLayout]; } - (NSNumber *)getWidth {