From 90e85a4adc749666f81034119f281ac54840e7df Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Thu, 16 Aug 2018 13:34:07 -0700 Subject: [PATCH] Implement 'decelerationRate' prop Summary: @public The content that renders within the `WKWebView` instance actually renders inside a `UIScrollView`. On that scroll view, we can adjust the `decelerationRate`, which controls how fast the view stops scrolling after the user lets go while scrolling. In this diff, I implemented the `decelerationRate` prop for `WKWebView`, which gets forwarded to the `UIScrollView` instance underlying the web view. **Note:** Even though we accept a floating point value for the deceleration rate, the native `UIScrollView` component only allows two inputs: 1. `UIScrollViewDecelerationRateNormal`: 0.998 2. `UIScrollViewDecelerationRateFast`: 0.99 As far as I know, it seems to just round up to the nearest valid `CGFloat` (or down if number > 0.998), for any invalid numbers. Reviewed By: mmmulani Differential Revision: D6307262 fbshipit-source-id: 98c4395702415aa36519f9e9bd84f043be3a5881 --- React/Views/RCTWKWebView.h | 1 + React/Views/RCTWKWebView.m | 9 ++++++++- React/Views/RCTWKWebViewManager.m | 4 ++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/React/Views/RCTWKWebView.h b/React/Views/RCTWKWebView.h index ca1d3ae1936ed7..c42023d09460c2 100644 --- a/React/Views/RCTWKWebView.h +++ b/React/Views/RCTWKWebView.h @@ -21,6 +21,7 @@ @property (nonatomic, assign) BOOL messagingEnabled; @property (nonatomic, copy) NSString *injectedJavaScript; @property (nonatomic, assign) BOOL scrollEnabled; +@property (nonatomic, assign) CGFloat decelerationRate; - (void)postMessage:(NSString *)message; diff --git a/React/Views/RCTWKWebView.m b/React/Views/RCTWKWebView.m index 63bab215932d7d..601651657d93f5 100644 --- a/React/Views/RCTWKWebView.m +++ b/React/Views/RCTWKWebView.m @@ -5,7 +5,7 @@ static NSString *const MessageHanderName = @"ReactNative"; -@interface RCTWKWebView () +@interface RCTWKWebView () @property (nonatomic, copy) RCTDirectEventBlock onLoadingStart; @property (nonatomic, copy) RCTDirectEventBlock onLoadingFinish; @property (nonatomic, copy) RCTDirectEventBlock onLoadingError; @@ -31,6 +31,7 @@ - (instancetype)initWithFrame:(CGRect)frame [wkWebViewConfig.userContentController addScriptMessageHandler: self name: MessageHanderName]; _webView = [[WKWebView alloc] initWithFrame:self.bounds configuration: wkWebViewConfig]; + _webView.scrollView.delegate = self; _webView.UIDelegate = self; _webView.navigationDelegate = self; [self addSubview:_webView]; @@ -85,6 +86,12 @@ - (void)setSource:(NSDictionary *)source } } + +- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView +{ + scrollView.decelerationRate = _decelerationRate; +} + - (void)setScrollEnabled:(BOOL)scrollEnabled { _webView.scrollView.scrollEnabled = scrollEnabled; diff --git a/React/Views/RCTWKWebViewManager.m b/React/Views/RCTWKWebViewManager.m index 5b71de0c7ac8b9..8c234b11536019 100644 --- a/React/Views/RCTWKWebViewManager.m +++ b/React/Views/RCTWKWebViewManager.m @@ -41,4 +41,8 @@ - (UIView *)view view.scrollEnabled = json == nil ? true : [RCTConvert BOOL: json]; } +RCT_CUSTOM_VIEW_PROPERTY(decelerationRate, CGFloat, RCTWKWebView) { + view.decelerationRate = json == nil ? UIScrollViewDecelerationRateNormal : [RCTConvert CGFloat: json]; +} + @end