[WEEX-271][iOS]Extend the pulldown WXRefreshComponent to support immediate load of the appear
WXRefreshComponent current only support the â¡, shuold add new type ofâ . // â refreshForAppear: load more when refresh component begin appear(if scroll is dragging or decelerating, should delay) // â¡refreshForWholeVisible: load more until the whole refresh component visible NSString *_refreshType; Improvementï¼271 Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/c1927668 Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/c1927668 Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/c1927668 Branch: refs/heads/master Commit: c1927668c72a81981ecf8dcb3046b4237fd8bc86 Parents: 9f75e24 Author: bbmjja8123 <bbmjja8...@163.com> Authored: Fri Mar 30 10:27:05 2018 +0800 Committer: bbmjja8123 <bbmjja8...@163.com> Committed: Fri Mar 30 10:27:05 2018 +0800 ---------------------------------------------------------------------- .../Sources/Component/WXRefreshComponent.h | 2 + .../Sources/Component/WXRefreshComponent.m | 41 ++++++++++++--- .../Sources/Component/WXScrollerComponent.m | 53 +++++++++++++++++++- .../Sources/Protocol/WXScrollerProtocol.h | 1 + 4 files changed, 88 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/c1927668/ios/sdk/WeexSDK/Sources/Component/WXRefreshComponent.h ---------------------------------------------------------------------- diff --git a/ios/sdk/WeexSDK/Sources/Component/WXRefreshComponent.h b/ios/sdk/WeexSDK/Sources/Component/WXRefreshComponent.h index a2c2b31..5fabfec 100644 --- a/ios/sdk/WeexSDK/Sources/Component/WXRefreshComponent.h +++ b/ios/sdk/WeexSDK/Sources/Component/WXRefreshComponent.h @@ -31,4 +31,6 @@ - (void)pullingdown:(NSDictionary*)param; +- (void)setIndicatorHidden:(BOOL)hidden; + @end http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/c1927668/ios/sdk/WeexSDK/Sources/Component/WXRefreshComponent.m ---------------------------------------------------------------------- diff --git a/ios/sdk/WeexSDK/Sources/Component/WXRefreshComponent.m b/ios/sdk/WeexSDK/Sources/Component/WXRefreshComponent.m index 67d2602..99876f4 100644 --- a/ios/sdk/WeexSDK/Sources/Component/WXRefreshComponent.m +++ b/ios/sdk/WeexSDK/Sources/Component/WXRefreshComponent.m @@ -148,15 +148,33 @@ if (scrollerProtocol == nil || !_initFinished) return; - CGPoint offset = [scrollerProtocol contentOffset]; - if (_displayState) { - offset.y = -self.calculatedFrame.size.height; - [_indicator start]; + if ([scrollerProtocol respondsToSelector:@selector(refreshType)] && + [[scrollerProtocol refreshType] isEqualToString:@"refreshForAppear"]) { + UIEdgeInsets inset = [scrollerProtocol contentInset]; + inset.top = self.calculatedFrame.size.height; + if (_displayState) { + if ([_indicator.view isHidden]) { + [_indicator.view setHidden:NO]; + } + [_indicator start]; + } else { + [_indicator stop]; + } + [scrollerProtocol setContentInset:inset]; } else { - offset.y = 0; - [_indicator stop]; + CGPoint offset = [scrollerProtocol contentOffset]; + if (_displayState) { + offset.y = -self.calculatedFrame.size.height; + if ([_indicator.view isHidden]) { + [_indicator.view setHidden:NO]; + } + [_indicator start]; + } else { + offset.y = 0; + [_indicator stop]; + } + [scrollerProtocol setContentOffset:offset animated:YES]; } - [scrollerProtocol setContentOffset:offset animated:YES]; } @@ -165,4 +183,13 @@ return _displayState; } +- (void)setIndicatorHidden:(BOOL)hidden { + [_indicator.view setHidden:hidden]; + if (!hidden) { + [_indicator start]; + } else { + [_indicator stop]; + } +} + @end http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/c1927668/ios/sdk/WeexSDK/Sources/Component/WXScrollerComponent.m ---------------------------------------------------------------------- diff --git a/ios/sdk/WeexSDK/Sources/Component/WXScrollerComponent.m b/ios/sdk/WeexSDK/Sources/Component/WXScrollerComponent.m index b0c8dae..c6f13a1 100644 --- a/ios/sdk/WeexSDK/Sources/Component/WXScrollerComponent.m +++ b/ios/sdk/WeexSDK/Sources/Component/WXScrollerComponent.m @@ -72,6 +72,10 @@ BOOL _scrollable; NSString * _alwaysScrollableVertical; NSString * _alwaysScrollableHorizontal; + + // refreshForAppear: load more when refresh component begin appear(if scroll is dragging or decelerating, should delay) + // refreshForWholeVisible: load more until the whole refresh component visible + NSString *_refreshType; // vertical & horizontal WXScrollDirection _scrollDirection; @@ -132,6 +136,7 @@ WX_EXPORT_METHOD(@selector(resetLoadmore)) if (attributes[@"alwaysScrollableHorizontal"]) { _alwaysScrollableHorizontal = [WXConvert NSString:attributes[@"alwaysScrollableHorizontal"]]; } + _refreshType = [WXConvert NSString:attributes[@"refreshType"]]?:@"refreshForWholeVisible"; _pagingEnabled = attributes[@"pagingEnabled"] ? [WXConvert BOOL:attributes[@"pagingEnabled"]] : NO; _loadMoreOffset = attributes[@"loadmoreoffset"] ? [WXConvert WXPixelType:attributes[@"loadmoreoffset"] scaleFactor:self.weexInstance.pixelScaleFactor] : 0; _loadmoreretry = attributes[@"loadmoreretry"] ? [WXConvert NSUInteger:attributes[@"loadmoreretry"]] : 0; @@ -263,6 +268,11 @@ WX_EXPORT_METHOD(@selector(resetLoadmore)) _alwaysScrollableVertical = [WXConvert NSString:attributes[@"alwaysScrollableVertical"]]; ((UIScrollView*)self.view).alwaysBounceVertical = [WXConvert BOOL:_alwaysScrollableVertical]; } + + if (attributes[@"refreshType"]) { + _refreshType = [WXConvert NSString:attributes[@"refreshType"]]; + } + if (attributes[@"offsetAccuracy"]) { _offsetAccuracy = [WXConvert WXPixelType:attributes[@"offsetAccuracy"] scaleFactor:self.weexInstance.pixelScaleFactor]; } @@ -535,9 +545,18 @@ WX_EXPORT_METHOD(@selector(resetLoadmore)) return _scrollDirection; } +- (NSString*)refreshType +{ + return _refreshType; +} + #pragma mark UIScrollViewDelegate - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { + if ([_refreshType isEqualToString:@"refreshForAppear"] && _refreshComponent) { + [_refreshComponent setIndicatorHidden:NO]; + } + if (_scrollStartEvent) { CGFloat scaleFactor = self.weexInstance.pixelScaleFactor; NSDictionary *contentSizeData = @{@"width":[NSNumber numberWithFloat:scrollView.contentSize.width / scaleFactor],@"height":[NSNumber numberWithFloat:scrollView.contentSize.height / scaleFactor]}; @@ -642,14 +661,34 @@ WX_EXPORT_METHOD(@selector(resetLoadmore)) } } } + +- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset +{ + if ([_refreshType isEqualToString:@"refreshForAppear"]) { + if(targetContentOffset == nil) + return; + CGPoint offset = *targetContentOffset; + if(velocity.y <= 0) { + // drop down + if( offset.y <= _refreshComponent.calculatedFrame.size.height ) { + [self loadMoreIfNeed]; + } + } else if (velocity.y > 0) { + // drop up + } + } +} + - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { [_loadingComponent.view setHidden:NO]; [_refreshComponent.view setHidden:NO]; //refresh - if (_refreshComponent && scrollView.contentOffset.y < 0 && scrollView.contentOffset.y + _refreshComponent.calculatedFrame.size.height < _refreshComponent.calculatedFrame.origin.y) { - [_refreshComponent refresh]; + if ([_refreshType isEqualToString:@"refreshForWholeVisible"]) { + if (_refreshComponent && scrollView.contentOffset.y < 0 && scrollView.contentOffset.y + _refreshComponent.calculatedFrame.size.height < _refreshComponent.calculatedFrame.origin.y) { + [_refreshComponent refresh]; + } } //loading @@ -663,6 +702,16 @@ WX_EXPORT_METHOD(@selector(resetLoadmore)) } } +- (void)loadMoreIfNeed +{ + WXScrollerComponnetView* scrollView = (WXScrollerComponnetView *)self.view; + if (scrollView.isDragging || scrollView.isTracking || scrollView.isDecelerating) { + [self performSelector:@selector(loadMoreIfNeed) withObject:nil afterDelay:0.1]; + return; + } + [_refreshComponent refresh]; +} + - (void)handleAppear { if (![self isViewLoaded]) { http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/c1927668/ios/sdk/WeexSDK/Sources/Protocol/WXScrollerProtocol.h ---------------------------------------------------------------------- diff --git a/ios/sdk/WeexSDK/Sources/Protocol/WXScrollerProtocol.h b/ios/sdk/WeexSDK/Sources/Protocol/WXScrollerProtocol.h index ae8428a..86ec39d 100644 --- a/ios/sdk/WeexSDK/Sources/Protocol/WXScrollerProtocol.h +++ b/ios/sdk/WeexSDK/Sources/Protocol/WXScrollerProtocol.h @@ -72,5 +72,6 @@ - (WXScrollDirection)scrollDirection; +- (NSString*)refreshType; @end