wqyfavor closed pull request #1924: [iOS] Fix `scrollStart` and `scrollEnd` can not be triggered when setting scroller's contentOffset URL: https://github.com/apache/incubator-weex/pull/1924
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/ios/sdk/WeexSDK/Sources/Component/RecycleList/WXRecycleListComponent.mm b/ios/sdk/WeexSDK/Sources/Component/RecycleList/WXRecycleListComponent.mm index f6de48b9a4..69e6f6b1ff 100644 --- a/ios/sdk/WeexSDK/Sources/Component/RecycleList/WXRecycleListComponent.mm +++ b/ios/sdk/WeexSDK/Sources/Component/RecycleList/WXRecycleListComponent.mm @@ -49,6 +49,28 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceive } } +- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated +{ + [super setContentOffset:contentOffset animated:animated]; + BOOL scrollStartEvent = [[self.wx_component valueForKey:@"_scrollStartEvent"] boolValue]; + id scrollEventListener = [self.wx_component valueForKey:@"_scrollEventListener"]; + + if (animated && (scrollStartEvent ||scrollEventListener) && !WXPointEqualToPoint(contentOffset, self.contentOffset)) { + CGFloat scaleFactor = self.wx_component.weexInstance.pixelScaleFactor; + NSDictionary *contentSizeData = @{@"width":@(self.contentSize.width / scaleFactor), + @"height":@(self.contentSize.height / scaleFactor)}; + NSDictionary *contentOffsetData = @{@"x":@(-self.contentOffset.x / scaleFactor), + @"y":@(-self.contentOffset.y / scaleFactor)}; + if (scrollStartEvent) { + [self.wx_component fireEvent:@"scrollstart" params:@{@"contentSize":contentSizeData, @"contentOffset":contentOffsetData} domChanges:nil]; + } + if (scrollEventListener) { + WXScrollerComponent *component = (WXScrollerComponent *)self.wx_component; + component.scrollEventListener(component, @"scrollstart", @{@"contentSize":contentSizeData, @"contentOffset":contentOffsetData}); + } + } +} + @end @interface WXRecycleListComponent () <WXRecycleListLayoutDelegate, WXRecycleListUpdateDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource> diff --git a/ios/sdk/WeexSDK/Sources/Component/WXListComponent.mm b/ios/sdk/WeexSDK/Sources/Component/WXListComponent.mm index c29aae0fee..2d35b61eb7 100644 --- a/ios/sdk/WeexSDK/Sources/Component/WXListComponent.mm +++ b/ios/sdk/WeexSDK/Sources/Component/WXListComponent.mm @@ -99,6 +99,28 @@ - (void)setFrame:(CGRect)frame { } } +- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated +{ + [super setContentOffset:contentOffset animated:animated]; + BOOL scrollStartEvent = [[self.wx_component valueForKey:@"_scrollStartEvent"] boolValue]; + id scrollEventListener = [self.wx_component valueForKey:@"_scrollEventListener"]; + + if (animated && (scrollStartEvent ||scrollEventListener) && !WXPointEqualToPoint(contentOffset, self.contentOffset)) { + CGFloat scaleFactor = self.wx_component.weexInstance.pixelScaleFactor; + NSDictionary *contentSizeData = @{@"width":@(self.contentSize.width / scaleFactor), + @"height":@(self.contentSize.height / scaleFactor)}; + NSDictionary *contentOffsetData = @{@"x":@(-self.contentOffset.x / scaleFactor), + @"y":@(-self.contentOffset.y / scaleFactor)}; + if (scrollStartEvent) { + [self.wx_component fireEvent:@"scrollstart" params:@{@"contentSize":contentSizeData, @"contentOffset":contentOffsetData} domChanges:nil]; + } + if (scrollEventListener) { + WXScrollerComponent *component = (WXScrollerComponent *)self.wx_component; + component.scrollEventListener(component, @"scrollstart", @{@"contentSize":contentSizeData, @"contentOffset":contentOffsetData}); + } + } +} + @end // WXText is a non-public is not permitted diff --git a/ios/sdk/WeexSDK/Sources/Component/WXScrollerComponent.mm b/ios/sdk/WeexSDK/Sources/Component/WXScrollerComponent.mm index 50256abfb5..42f0ee2cb8 100644 --- a/ios/sdk/WeexSDK/Sources/Component/WXScrollerComponent.mm +++ b/ios/sdk/WeexSDK/Sources/Component/WXScrollerComponent.mm @@ -32,6 +32,7 @@ #import "WXComponent+Events.h" #import "WXPageEventNotifyEvent.h" #import "WXComponent+Layout.h" +#import "WXUtility.h" @interface WXScrollerComponentView : UIScrollView @end @@ -46,6 +47,29 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceive return YES; } } + +- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated +{ + [super setContentOffset:contentOffset animated:animated]; + BOOL scrollStartEvent = [[self.wx_component valueForKey:@"_scrollStartEvent"] boolValue]; + id scrollEventListener = [self.wx_component valueForKey:@"_scrollEventListener"]; + + if (animated && (scrollStartEvent ||scrollEventListener) && !WXPointEqualToPoint(contentOffset, self.contentOffset)) { + CGFloat scaleFactor = self.wx_component.weexInstance.pixelScaleFactor; + NSDictionary *contentSizeData = @{@"width":@(self.contentSize.width / scaleFactor), + @"height":@(self.contentSize.height / scaleFactor)}; + NSDictionary *contentOffsetData = @{@"x":@(-self.contentOffset.x / scaleFactor), + @"y":@(-self.contentOffset.y / scaleFactor)}; + if (scrollStartEvent) { + [self.wx_component fireEvent:@"scrollstart" params:@{@"contentSize":contentSizeData, @"contentOffset":contentOffsetData} domChanges:nil]; + } + if (scrollEventListener) { + WXScrollerComponent *component = (WXScrollerComponent *)self.wx_component; + component.scrollEventListener(component, @"scrollstart", @{@"contentSize":contentSizeData, @"contentOffset":contentOffsetData}); + } + } +} + @end @interface WXScrollToTarget : NSObject @@ -201,7 +225,7 @@ -(instancetype)initWithRef:(NSString *)ref type:(NSString *)type styles:(NSDicti //may be list if ([@"scroller" isEqualToString:type]) { [weexInstance.apmInstance updateDiffStats:KEY_PAGE_STATS_SCROLLER_NUM withDiffValue:1]; - } + } } return self; @@ -775,6 +799,8 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { + [self dispatchScrollEndEvent:scrollView]; + UIEdgeInsets inset = [scrollView contentInset]; // currently only set contentInset when loading @@ -802,24 +828,8 @@ - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { - if (_scrollEndEvent || _scrollEventListener) { - if (!_isScrolling) { - CGFloat scaleFactor = self.weexInstance.pixelScaleFactor; - NSDictionary *contentSizeData = @{@"width":@(scrollView.contentSize.width / scaleFactor), - @"height":@(scrollView.contentSize.height / scaleFactor)}; - NSDictionary *contentOffsetData = @{@"x":@(-scrollView.contentOffset.x / scaleFactor), - @"y":@(-scrollView.contentOffset.y / scaleFactor)}; - - if (_scrollEndEvent) { - [self fireEvent:@"scrollend" params:@{@"contentSize":contentSizeData, @"contentOffset":contentOffsetData} domChanges:nil]; - } - if (_scrollEventListener) { - _scrollEventListener(self, @"scrollend", @{@"contentSize":contentSizeData, @"contentOffset":contentOffsetData}); - } - } - } - if (!_isScrolling) { + [self dispatchScrollEndEvent:scrollView]; _scrollEndPoint = scrollView.contentOffset; id<WXPageEventNotifyEventProtocol> eventNotify = [WXSDKEngine handlerForProtocol:@protocol(WXPageEventNotifyEventProtocol)]; if ([eventNotify respondsToSelector:@selector(notifyScrollEvent:from:to:)]) { @@ -984,6 +994,22 @@ - (CGPoint)absolutePositionForComponent:(WXComponent *)component } #pragma mark Private Methods +- (void)dispatchScrollEndEvent:(UIScrollView *)scrollView +{ + if (_scrollEndEvent || _scrollEventListener) { + CGFloat scaleFactor = self.weexInstance.pixelScaleFactor; + NSDictionary *contentSizeData = @{@"width":@(scrollView.contentSize.width / scaleFactor), + @"height":@(scrollView.contentSize.height / scaleFactor)}; + NSDictionary *contentOffsetData = @{@"x":@(-scrollView.contentOffset.x / scaleFactor), + @"y":@(-scrollView.contentOffset.y / scaleFactor)}; + if (_scrollEndEvent) { + [self fireEvent:@"scrollend" params:@{@"contentSize":contentSizeData, @"contentOffset":contentOffsetData} domChanges:nil]; + } + if (_scrollEventListener) { + _scrollEventListener(self, @"scrollend", @{@"contentSize":contentSizeData, @"contentOffset":contentOffsetData}); + } + } +} - (void)scrollToTarget:(WXScrollToTarget *)target scrollRect:(CGRect)rect { diff --git a/ios/sdk/WeexSDK/Sources/Utility/WXUtility.h b/ios/sdk/WeexSDK/Sources/Utility/WXUtility.h index 730c56080b..f6acabd765 100644 --- a/ios/sdk/WeexSDK/Sources/Utility/WXUtility.h +++ b/ios/sdk/WeexSDK/Sources/Utility/WXUtility.h @@ -81,6 +81,13 @@ do {\ }\ }while(0) +#define WXPointEqualToPoint __WXPointEqualToPoint +CG_INLINE bool +__WXPointEqualToPoint(CGPoint point1, CGPoint point2) +{ + return fabs (point1.x - point2.x) < 0.00001 && fabs (point1.y - point2.y) < 0.00001; +} + #ifdef __cplusplus extern "C" { #endif ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services