From fb900f7d6665be174afc04ad10326aa856411ea9 Mon Sep 17 00:00:00 2001 From: faimin Date: Sat, 17 Jul 2021 21:49:11 +0800 Subject: [PATCH] set view hierarchy lazily --- Source/Core/Public/UIView+ZDFlexLayout.m | 22 +----------- Source/Core/Public/ZDFlexLayout.m | 22 ++++++++++++ Source/Core/Public/ZDFlexLayoutDiv.m | 43 ++++++------------------ 3 files changed, 34 insertions(+), 53 deletions(-) diff --git a/Source/Core/Public/UIView+ZDFlexLayout.m b/Source/Core/Public/UIView+ZDFlexLayout.m index 91da332..284bcbb 100644 --- a/Source/Core/Public/UIView+ZDFlexLayout.m +++ b/Source/Core/Public/UIView+ZDFlexLayout.m @@ -124,15 +124,7 @@ - (void)removeChild:(ZDFlexLayoutView)child { } [self.children removeObject:child]; - - if ([child isKindOfClass:UIView.class]) { - [(UIView *)child removeFromSuperview]; - } - else { - [child.children enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(ZDFlexLayoutView _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - [child removeChild:obj]; - }]; - } + child.parent = nil; child.owningView = nil; @@ -164,18 +156,6 @@ - (void)insertChild:(ZDFlexLayoutView)child atIndex:(NSInteger)index { [self.children insertObject:child atIndex:mapedIndex]; child.parent = self; child.owningView = self; - - if ([child isKindOfClass:UIView.class]) { - [self insertSubview:(UIView *)child atIndex:mapedIndex]; - } - else { - for (ZDFlexLayoutView childChild in child.children) { - childChild.owningView = self; - if ([childChild isKindOfClass:UIView.class]) { - [self insertSubview:(UIView *)childChild atIndex:mapedIndex++]; - } - } - } } - (void)removeFromParent { diff --git a/Source/Core/Public/ZDFlexLayout.m b/Source/Core/Public/ZDFlexLayout.m index 72f7568..f851869 100644 --- a/Source/Core/Public/ZDFlexLayout.m +++ b/Source/Core/Public/ZDFlexLayout.m @@ -502,6 +502,28 @@ static void YGApplyLayoutToViewHierarchy(ZDFlexLayoutView view, BOOL preserveOri [view needReApplyLayoutAtNextRunloop]; } } + + ZDResetViewHierarchy(view); +} + +/// lazy set view tree +static void ZDResetViewHierarchy(ZDFlexLayoutView view) +{ + if (!view) { + return; + } + + UIView *superView = [view isKindOfClass:UIView.self] ? (UIView *)view : view.owningView; + NSCAssert(superView != nil, @"view can't be nil"); + + for (ZDFlexLayoutView child in view.children) { + if ([child isKindOfClass:UIView.self]) { + [superView addSubview:(UIView *)child]; + } + else { + ZDResetViewHierarchy(child); + } + } } static void ZD_Dispatch_sync_on_main_queue(dispatch_block_t block) diff --git a/Source/Core/Public/ZDFlexLayoutDiv.m b/Source/Core/Public/ZDFlexLayoutDiv.m index fbf717c..5764e1e 100644 --- a/Source/Core/Public/ZDFlexLayoutDiv.m +++ b/Source/Core/Public/ZDFlexLayoutDiv.m @@ -50,15 +50,6 @@ - (void)removeChild:(ZDFlexLayoutView)child { [self.children removeObject:child]; - if ([child isKindOfClass:UIView.class]) { - [(UIView *)child removeFromSuperview]; - } - else { - [child.children enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(ZDFlexLayoutView _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - [child removeChild:obj]; - }]; - } - child.parent = nil; } @@ -88,18 +79,6 @@ - (void)insertChild:(ZDFlexLayoutView)child atIndex:(NSInteger)index { [self.children insertObject:child atIndex:mapedIndex]; child.parent = self; child.owningView = _owningView; - - if ([child isKindOfClass:UIView.class]) { - [_owningView insertSubview:(UIView *)child atIndex:mapedIndex]; - } - else { - for (ZDFlexLayoutView childChild in child.children) { - childChild.owningView = child.owningView; - if ([childChild isKindOfClass:UIView.class]) { - [_owningView insertSubview:(UIView *)childChild atIndex:mapedIndex++]; - } - } - } } - (void)removeFromParent { @@ -193,17 +172,17 @@ - (void)addChildSubviews:(ZDFlexLayoutView)child { child.owningView = _owningView; - if ([child isKindOfClass:UIView.class]) { - [_owningView addSubview:(UIView *)child]; - } - else { - for (ZDFlexLayoutView childChild in child.children) { - childChild.owningView = child.owningView; - if ([childChild isKindOfClass:UIView.class]) { - [_owningView addSubview:(UIView *)childChild]; - } - } - } +// if ([child isKindOfClass:UIView.class]) { +// [_owningView addSubview:(UIView *)child]; +// } +// else { +// for (ZDFlexLayoutView childChild in child.children) { +// childChild.owningView = child.owningView; +// if ([childChild isKindOfClass:UIView.class]) { +// [_owningView addSubview:(UIView *)childChild]; +// } +// } +// } } @end