Permalink
Browse files

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
  • Loading branch information...
RSNara authored and kelset committed Aug 16, 2018
1 parent 1c3af59 commit 566259880a9c866c0fce6a3cd199b2011c4ead44
Showing with 13 additions and 1 deletion.
  1. +1 −0 React/Views/RCTWKWebView.h
  2. +8 −1 React/Views/RCTWKWebView.m
  3. +4 −0 React/Views/RCTWKWebViewManager.m
@@ -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;
@@ -5,7 +5,7 @@
static NSString *const MessageHanderName = @"ReactNative";
@interface RCTWKWebView () <WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler>
@interface RCTWKWebView () <WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler, UIScrollViewDelegate>
@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;
@@ -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

0 comments on commit 5662598

Please sign in to comment.