mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-24 04:16:00 +08:00
UIRefreshControl added to scroll view
Summary: **What:** adds `onRefreshStart` property to `ScrollView.js` for displaying and activating pull to refresh. **Why:** Javascript implementations seemed a little flakey and inconsistent. As you can see in the issues below: https://github.com/facebook/react-native/issues/2356 https://github.com/facebook/react-native/issues/745 So this is an attempt a completely native implementation. What do you think?  Closes https://github.com/facebook/react-native/pull/4205 Reviewed By: svcscm Differential Revision: D2674945 Pulled By: nicklockwood fb-gh-sync-id: 65113a5db9785df5a95c68323c2cdf19f3b217b1
This commit is contained in:
committed by
facebook-github-bot-4
parent
5950f8cf15
commit
2faf8632d3
@@ -47,6 +47,9 @@
|
||||
@property (nonatomic, assign) int snapToInterval;
|
||||
@property (nonatomic, copy) NSString *snapToAlignment;
|
||||
@property (nonatomic, copy) NSIndexSet *stickyHeaderIndices;
|
||||
@property (nonatomic, copy) RCTDirectEventBlock onRefreshStart;
|
||||
|
||||
- (void)endRefreshing;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -144,6 +144,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
||||
|
||||
@property (nonatomic, copy) NSIndexSet *stickyHeaderIndices;
|
||||
@property (nonatomic, assign) BOOL centerContent;
|
||||
@property (nonatomic, strong) UIRefreshControl *refreshControl;
|
||||
|
||||
@end
|
||||
|
||||
@@ -352,6 +353,15 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
||||
return hitView ?: [super hitTest:point withEvent:event];
|
||||
}
|
||||
|
||||
- (void)setRefreshControl:(UIRefreshControl *)refreshControl
|
||||
{
|
||||
if (_refreshControl) {
|
||||
[_refreshControl removeFromSuperview];
|
||||
}
|
||||
_refreshControl = refreshControl;
|
||||
[self addSubview:_refreshControl];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation RCTScrollView
|
||||
@@ -844,6 +854,34 @@ RCT_SET_AND_PRESERVE_OFFSET(setScrollIndicatorInsets, UIEdgeInsets);
|
||||
return [_scrollView valueForKey:key];
|
||||
}
|
||||
|
||||
- (void)setOnRefreshStart:(RCTDirectEventBlock)onRefreshStart
|
||||
{
|
||||
if (!onRefreshStart) {
|
||||
_onRefreshStart = nil;
|
||||
_scrollView.refreshControl = nil;
|
||||
return;
|
||||
}
|
||||
_onRefreshStart = [onRefreshStart copy];
|
||||
|
||||
if (!_scrollView.refreshControl) {
|
||||
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
|
||||
[refreshControl addTarget:self action:@selector(refreshControlValueChanged) forControlEvents:UIControlEventValueChanged];
|
||||
_scrollView.refreshControl = refreshControl;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)refreshControlValueChanged
|
||||
{
|
||||
if (self.onRefreshStart) {
|
||||
self.onRefreshStart(nil);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)endRefreshing
|
||||
{
|
||||
[_scrollView.refreshControl endRefreshing];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation RCTEventDispatcher (RCTScrollView)
|
||||
|
||||
@@ -65,6 +65,7 @@ RCT_EXPORT_VIEW_PROPERTY(scrollIndicatorInsets, UIEdgeInsets)
|
||||
RCT_EXPORT_VIEW_PROPERTY(snapToInterval, int)
|
||||
RCT_EXPORT_VIEW_PROPERTY(snapToAlignment, NSString)
|
||||
RCT_REMAP_VIEW_PROPERTY(contentOffset, scrollView.contentOffset, CGPoint)
|
||||
RCT_EXPORT_VIEW_PROPERTY(onRefreshStart, RCTDirectEventBlock)
|
||||
|
||||
- (NSDictionary<NSString *, id> *)constantsToExport
|
||||
{
|
||||
@@ -114,6 +115,22 @@ RCT_EXPORT_METHOD(calculateChildFrames:(nonnull NSNumber *)reactTag
|
||||
}];
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(endRefreshing:(nonnull NSNumber *)reactTag)
|
||||
{
|
||||
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTScrollView *> *viewRegistry) {
|
||||
|
||||
RCTScrollView *view = viewRegistry[reactTag];
|
||||
if (!view || ![view isKindOfClass:[RCTScrollView class]]) {
|
||||
RCTLogError(@"Cannot find RCTScrollView with tag #%@", reactTag);
|
||||
return;
|
||||
}
|
||||
|
||||
[view endRefreshing];
|
||||
|
||||
}];
|
||||
}
|
||||
|
||||
|
||||
- (NSArray<NSString *> *)customDirectEventTypes
|
||||
{
|
||||
return @[
|
||||
@@ -127,3 +144,4 @@ RCT_EXPORT_METHOD(calculateChildFrames:(nonnull NSNumber *)reactTag
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user