mirror of
https://github.com/status-im/react-native.git
synced 2025-01-18 05:23:26 +00:00
2bb1c263db
Summary: Currently the Modal component uses the slide up / down animation for presenting and hiding the Modal with no options. This PR gives users a choice to use a fade in / out animation or the current slide animation (slide is the default). Android and iOS. ![](http://g.recordit.co/nfJSg487Ox.gif) ![](http://g.recordit.co/QHGDuUFbPy.gif) I've updated the UIExplorer and documentation. ![image](https://cloud.githubusercontent.com/assets/4265163/14743130/0bd8282c-086e-11e6-93eb-3d344431337d.png) Thanks! Closes https://github.com/facebook/react-native/pull/7156 Differential Revision: D3237809 Pulled By: javache fb-gh-sync-id: 813e56ada8b19990dc5018527dc3a81b2c8b349a fbshipit-source-id: 813e56ada8b19990dc5018527dc3a81b2c8b349a
143 lines
3.9 KiB
Objective-C
143 lines
3.9 KiB
Objective-C
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*/
|
|
|
|
#import "RCTModalHostView.h"
|
|
|
|
#import "RCTAssert.h"
|
|
#import "RCTBridge.h"
|
|
#import "RCTModalHostViewController.h"
|
|
#import "RCTTouchHandler.h"
|
|
#import "RCTUIManager.h"
|
|
#import "UIView+React.h"
|
|
|
|
@implementation RCTModalHostView
|
|
{
|
|
__weak RCTBridge *_bridge;
|
|
BOOL _isPresented;
|
|
RCTModalHostViewController *_modalViewController;
|
|
RCTTouchHandler *_touchHandler;
|
|
UIView *_reactSubview;
|
|
}
|
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:coder)
|
|
|
|
- (instancetype)initWithBridge:(RCTBridge *)bridge
|
|
{
|
|
if ((self = [super initWithFrame:CGRectZero])) {
|
|
_bridge = bridge;
|
|
_modalViewController = [RCTModalHostViewController new];
|
|
UIView *containerView = [UIView new];
|
|
containerView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
|
|
_modalViewController.view = containerView;
|
|
_touchHandler = [[RCTTouchHandler alloc] initWithBridge:bridge];
|
|
_isPresented = NO;
|
|
|
|
__weak typeof(self) weakSelf = self;
|
|
_modalViewController.boundsDidChangeBlock = ^(CGRect newBounds) {
|
|
[weakSelf notifyForBoundsChange:newBounds];
|
|
};
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
- (void)notifyForBoundsChange:(CGRect)newBounds
|
|
{
|
|
if (_reactSubview && _isPresented) {
|
|
[_bridge.uiManager setFrame:newBounds forView:_reactSubview];
|
|
}
|
|
}
|
|
|
|
- (NSArray<UIView *> *)reactSubviews
|
|
{
|
|
return _reactSubview ? @[_reactSubview] : @[];
|
|
}
|
|
|
|
- (void)insertReactSubview:(UIView *)subview atIndex:(__unused NSInteger)atIndex
|
|
{
|
|
RCTAssert(_reactSubview == nil, @"Modal view can only have one subview");
|
|
[subview addGestureRecognizer:_touchHandler];
|
|
subview.autoresizingMask = UIViewAutoresizingFlexibleHeight |
|
|
UIViewAutoresizingFlexibleWidth;
|
|
|
|
[_modalViewController.view insertSubview:subview atIndex:0];
|
|
_reactSubview = subview;
|
|
}
|
|
|
|
- (void)removeReactSubview:(UIView *)subview
|
|
{
|
|
RCTAssert(subview == _reactSubview, @"Cannot remove view other than modal view");
|
|
[subview removeGestureRecognizer:_touchHandler];
|
|
[subview removeFromSuperview];
|
|
_reactSubview = nil;
|
|
}
|
|
|
|
- (void)dismissModalViewController
|
|
{
|
|
if (_isPresented) {
|
|
[_modalViewController dismissViewControllerAnimated:[self hasAnimationType] completion:nil];
|
|
_isPresented = NO;
|
|
}
|
|
}
|
|
|
|
- (void)didMoveToWindow
|
|
{
|
|
[super didMoveToWindow];
|
|
|
|
if (!_isPresented && self.window) {
|
|
RCTAssert(self.reactViewController, @"Can't present modal view controller without a presenting view controller");
|
|
|
|
if ([self.animationType isEqualToString:@"fade"]) {
|
|
_modalViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
|
|
} else if ([self.animationType isEqualToString:@"slide"]) {
|
|
_modalViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
|
|
}
|
|
[self.reactViewController presentViewController:_modalViewController animated:[self hasAnimationType] completion:^{
|
|
if (_onShow) {
|
|
_onShow(nil);
|
|
}
|
|
}];
|
|
_isPresented = YES;
|
|
}
|
|
}
|
|
|
|
- (void)didMoveToSuperview
|
|
{
|
|
[super didMoveToSuperview];
|
|
|
|
if (_isPresented && !self.superview) {
|
|
[self dismissModalViewController];
|
|
}
|
|
}
|
|
|
|
- (void)invalidate
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
[self dismissModalViewController];
|
|
});
|
|
}
|
|
|
|
- (BOOL)isTransparent
|
|
{
|
|
return _modalViewController.modalPresentationStyle == UIModalPresentationCustom;
|
|
}
|
|
|
|
- (BOOL)hasAnimationType
|
|
{
|
|
return ![self.animationType isEqualToString:@"none"];
|
|
}
|
|
|
|
- (void)setTransparent:(BOOL)transparent
|
|
{
|
|
_modalViewController.modalPresentationStyle = transparent ? UIModalPresentationCustom : UIModalPresentationFullScreen;
|
|
}
|
|
|
|
@end
|