Skip to content

Commit

Permalink
feat(ios): add onsizechanged event
Browse files Browse the repository at this point in the history
  • Loading branch information
ozonelmy authored and zoomchan-cxj committed Mar 16, 2023
1 parent 1c0d713 commit 1b84c10
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 12 deletions.
9 changes: 6 additions & 3 deletions ios/sdk/base/HippyRootView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,12 @@ - (void)insertHippySubview:(UIView *)subview atIndex:(NSInteger)atIndex {
}

- (void)setFrame:(CGRect)frame {
super.frame = frame;
if (self.hippyTag && _bridge.isValid) {
[_bridge.uiManager setFrame:frame forView:self];
CGRect originFrame = self.frame;
if (!CGRectEqualToRect(originFrame, frame)) {
super.frame = frame;
if (self.hippyTag && _bridge.isValid) {
[_bridge.uiManager setFrame:frame fromOriginFrame:originFrame forView:self];
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion ios/sdk/component/modal/HippyModalHostView.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@
@property (nonatomic, readonly) HippyTouchHandler *touchHandler;

- (instancetype)initWithBridge:(HippyBridge *)bridge NS_DESIGNATED_INITIALIZER;
- (void)notifyForBoundsChange:(CGRect)newBounds;
- (void)notifyForBoundsChange:(CGRect)newBounds oldBounds:(CGRect)oldBounds;

@end
11 changes: 7 additions & 4 deletions ios/sdk/component/modal/HippyModalHostView.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,20 @@ - (instancetype)initWithBridge:(HippyBridge *)bridge {
_isPresented = NO;

__weak __typeof(self) weakSelf = self;
_modalViewController.boundsDidChangeBlock = ^(CGRect newBounds) {
[weakSelf notifyForBoundsChange:newBounds];
_modalViewController.boundsDidChangeBlock = ^(CGRect oldBounds, CGRect newBounds) {
HippyModalHostView *strongSelf = weakSelf;
if (strongSelf) {
[weakSelf notifyForBoundsChange:newBounds oldBounds:oldBounds];
}
};
}

return self;
}

- (void)notifyForBoundsChange:(CGRect)newBounds {
- (void)notifyForBoundsChange:(CGRect)newBounds oldBounds:(CGRect)oldBounds {
if (_hippySubview && _isPresented) {
[_bridge.uiManager setFrame:newBounds forView:_hippySubview];
[_bridge.uiManager setFrame:newBounds fromOriginFrame:oldBounds forView:_hippySubview];
[self notifyForOrientationChange];
}
}
Expand Down
2 changes: 1 addition & 1 deletion ios/sdk/component/modal/HippyModalHostViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

@interface HippyModalHostViewController : UIViewController

@property (nonatomic, copy) void (^boundsDidChangeBlock)(CGRect newBounds);
@property (nonatomic, copy) void (^boundsDidChangeBlock)(CGRect oldBounds, CGRect newBounds);
@property (nonatomic, strong) NSNumber *hideStatusBar;
@property (nonatomic, assign) UIInterfaceOrientationMask supportedInterfaceOrientations;
@end
2 changes: 1 addition & 1 deletion ios/sdk/component/modal/HippyModalHostViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ - (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];

if (self.boundsDidChangeBlock && !CGRectEqualToRect(_lastViewFrame, self.view.frame)) {
self.boundsDidChangeBlock(self.view.bounds);
self.boundsDidChangeBlock(_lastViewFrame, self.view.bounds);
_lastViewFrame = self.view.frame;
}
}
Expand Down
2 changes: 1 addition & 1 deletion ios/sdk/module/uimanager/HippyUIManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ HIPPY_EXTERN NSString *const HippyUIManagerDidEndBatchNotification;
* Update the frame of a view. This might be in response to a screen rotation
* or some other layout event outside of the Hippy-managed view hierarchy.
*/
- (void)setFrame:(CGRect)frame forView:(UIView *)view;
- (void)setFrame:(CGRect)frame fromOriginFrame:(CGRect)originFrame forView:(UIView *)view;

/**
* Set the natural size of a view, which is used when no explicit size is set.
Expand Down
7 changes: 6 additions & 1 deletion ios/sdk/module/uimanager/HippyUIManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ - (HippyVirtualNode *)nodeForHippyTag:(NSNumber *)hippyTag
return _nodeRegistry[hippyTag];
}

- (void)setFrame:(CGRect)frame forView:(UIView *)view
- (void)setFrame:(CGRect)frame fromOriginFrame:(CGRect)originFrame forView:(UIView *)view
{
HippyAssertMainQueue();

Expand All @@ -328,6 +328,11 @@ - (void)setFrame:(CGRect)frame forView:(UIView *)view
if (rootView != nil) {
sizeFlexibility = rootView.sizeFlexibility;
isRootView = YES;
NSDictionary *params = @{@"oldWidth": @(CGRectGetWidth(originFrame)), @"oldHeight": @(CGRectGetHeight(originFrame)),
@"width": @(CGRectGetWidth(frame)), @"height": @(CGRectGetHeight(frame))
};
NSDictionary *args = @{@"eventName": @"onSizeChanged", @"extra": params};
[[[self bridge] eventDispatcher] dispatchEvent:@"EventDispatcher" methodName:@"receiveNativeEvent" args:args];
}
}

Expand Down

0 comments on commit 1b84c10

Please sign in to comment.