Skip to content

Commit 90e85a4

Browse files
RSNarafacebook-github-bot
authored andcommitted
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
1 parent 1741fe9 commit 90e85a4

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

React/Views/RCTWKWebView.h

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
@property (nonatomic, assign) BOOL messagingEnabled;
2222
@property (nonatomic, copy) NSString *injectedJavaScript;
2323
@property (nonatomic, assign) BOOL scrollEnabled;
24+
@property (nonatomic, assign) CGFloat decelerationRate;
2425

2526
- (void)postMessage:(NSString *)message;
2627

React/Views/RCTWKWebView.m

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
static NSString *const MessageHanderName = @"ReactNative";
77

8-
@interface RCTWKWebView () <WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler>
8+
@interface RCTWKWebView () <WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler, UIScrollViewDelegate>
99
@property (nonatomic, copy) RCTDirectEventBlock onLoadingStart;
1010
@property (nonatomic, copy) RCTDirectEventBlock onLoadingFinish;
1111
@property (nonatomic, copy) RCTDirectEventBlock onLoadingError;
@@ -31,6 +31,7 @@ - (instancetype)initWithFrame:(CGRect)frame
3131
[wkWebViewConfig.userContentController addScriptMessageHandler: self name: MessageHanderName];
3232

3333
_webView = [[WKWebView alloc] initWithFrame:self.bounds configuration: wkWebViewConfig];
34+
_webView.scrollView.delegate = self;
3435
_webView.UIDelegate = self;
3536
_webView.navigationDelegate = self;
3637
[self addSubview:_webView];
@@ -85,6 +86,12 @@ - (void)setSource:(NSDictionary *)source
8586
}
8687
}
8788

89+
90+
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
91+
{
92+
scrollView.decelerationRate = _decelerationRate;
93+
}
94+
8895
- (void)setScrollEnabled:(BOOL)scrollEnabled
8996
{
9097
_webView.scrollView.scrollEnabled = scrollEnabled;

React/Views/RCTWKWebViewManager.m

+4
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ - (UIView *)view
4141
view.scrollEnabled = json == nil ? true : [RCTConvert BOOL: json];
4242
}
4343

44+
RCT_CUSTOM_VIEW_PROPERTY(decelerationRate, CGFloat, RCTWKWebView) {
45+
view.decelerationRate = json == nil ? UIScrollViewDecelerationRateNormal : [RCTConvert CGFloat: json];
46+
}
47+
4448
@end

0 commit comments

Comments
 (0)