From 050dfc46ae82e2fe3f7a4042a7961743c94e6175 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sat, 28 Mar 2020 12:03:35 +0800 Subject: [PATCH] iOS:Fix onFrameChangedCallback not functional right --- doric-iOS/Pod/Classes/DoricPanel.m | 26 ++++++++-------- doric-iOS/Pod/Classes/DoricViewController.m | 8 +---- doric-iOS/Pod/Classes/Shader/DoricRootNode.h | 6 ++-- doric-iOS/Pod/Classes/Shader/DoricRootNode.m | 31 ++++++++++++++++++++ 4 files changed, 48 insertions(+), 23 deletions(-) diff --git a/doric-iOS/Pod/Classes/DoricPanel.m b/doric-iOS/Pod/Classes/DoricPanel.m index cc4918b9..be5a743c 100644 --- a/doric-iOS/Pod/Classes/DoricPanel.m +++ b/doric-iOS/Pod/Classes/DoricPanel.m @@ -29,10 +29,21 @@ @implementation DoricPanel - (void)config:(NSString *)script alias:(NSString *)alias extra:(NSString *)extra { self.doricContext = [[[DoricContext alloc] initWithScript:script source:alias extra:extra] also:^(DoricContext *it) { - [it.rootNode setupRootView:[[DoricStackView new] also:^(DoricStackView *it) { + [it.rootNode setupRootView:[[DoricRootView new] also:^(DoricRootView *it) { it.width = self.view.width; it.height = self.view.height; it.clipsToBounds = YES; + __weak typeof(self) __self = self; + it.frameChangedBlock = ^(CGSize oldSize, CGSize newSize) { + __strong typeof(__self) self = __self; + self.renderedWidth = newSize.width; + self.renderedHeight = newSize.height; + self.view.width = newSize.width; + self.view.height = newSize.height; + if (self.frameChangedBlock) { + self.frameChangedBlock(newSize); + } + }; [self.view addSubview:it]; }]]; }]; @@ -41,21 +52,10 @@ - (void)config:(NSString *)script alias:(NSString *)alias extra:(NSString *)extr - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; - if (self.doricContext && self.renderedWidth != self.view.width && self.renderedHeight != self.view.height) { + if (self.doricContext && (self.renderedWidth != self.view.width || self.renderedHeight != self.view.height)) { self.renderedWidth = self.view.width; self.renderedHeight = self.view.height; [self.doricContext build:CGSizeMake(self.renderedWidth, self.renderedHeight)]; - } else { - [self.doricContext.rootNode.view also:^(DoricStackView *it) { - if (it.width != self.renderedWidth || it.height != self.renderedHeight) { - // Frame changed - self.renderedWidth = self.view.width = it.width; - self.renderedHeight = self.view.height = it.height; - if (self.frameChangedBlock) { - self.frameChangedBlock(CGSizeMake(it.width, it.height)); - } - } - }]; } } diff --git a/doric-iOS/Pod/Classes/DoricViewController.m b/doric-iOS/Pod/Classes/DoricViewController.m index 3b7749ac..f0dcb252 100644 --- a/doric-iOS/Pod/Classes/DoricViewController.m +++ b/doric-iOS/Pod/Classes/DoricViewController.m @@ -48,9 +48,9 @@ - (instancetype)initWithSource:(NSString *)source alias:(NSString *)alias extra: - (void)viewDidLoad { [super viewDidLoad]; + self.view.backgroundColor = [UIColor whiteColor]; self.doricPanel = [[DoricPanel new] also:^(DoricPanel *it) { [it.view also:^(UIView *it) { - it.backgroundColor = [UIColor whiteColor]; it.width = self.view.width; it.height = self.view.height; }]; @@ -99,12 +99,6 @@ - (void)viewWillDisappear:(BOOL)animated { } } -- (void)viewWillLayoutSubviews { - [super viewWillLayoutSubviews]; - self.doricPanel.view.width = self.view.width; - self.doricPanel.view.height = self.view.height; -} - - (void)doric_navigator_push:(NSString *)source alias:(NSString *)alias animated:(BOOL)animated extra:(NSString *)extra { DoricViewController *viewController = [[DoricViewController alloc] initWithSource:source alias:alias extra:extra]; [self.navigationController pushViewController:viewController animated:animated]; diff --git a/doric-iOS/Pod/Classes/Shader/DoricRootNode.h b/doric-iOS/Pod/Classes/Shader/DoricRootNode.h index 32abe898..2380c4a0 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricRootNode.h +++ b/doric-iOS/Pod/Classes/Shader/DoricRootNode.h @@ -22,12 +22,12 @@ #import "DoricStackNode.h" -NS_ASSUME_NONNULL_BEGIN +@interface DoricRootView : DoricStackView +@property(nonatomic, strong) void (^frameChangedBlock)(CGSize oldSize, CGSize newSize); +@end @interface DoricRootNode : DoricStackNode - (void)setupRootView:(DoricStackView *)view; @end - -NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Pod/Classes/Shader/DoricRootNode.m b/doric-iOS/Pod/Classes/Shader/DoricRootNode.m index 669c6bbb..01e644f2 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricRootNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricRootNode.m @@ -22,6 +22,37 @@ #import "DoricRootNode.h" +@interface DoricRootView () +@property(nonatomic, assign) CGSize currentSize; +@end + +@implementation DoricRootView + +- (instancetype)init { + if (self = [super init]) { + _currentSize = self.frame.size; + } + return self; +} + +- (void)layoutSelf:(CGSize)targetSize { + [super layoutSelf:targetSize]; + if (!CGSizeEqualToSize(self.currentSize, targetSize) && self.frameChangedBlock) { + self.frameChangedBlock(self.currentSize, targetSize); + } + self.currentSize = targetSize; +} + +- (void)setX:(CGFloat)x { + self.superview.x = x; +} + +- (void)setY:(CGFloat)y { + self.superview.y = y; +} + +@end + @implementation DoricRootNode - (void)setupRootView:(DoricStackView *)view { self.view = view;