Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
[iOS] Fix scrollStart and scrollEnd can not be triggered when set…
Browse files Browse the repository at this point in the history
…ting scroller's contentOffset
  • Loading branch information
千之 committed Dec 18, 2018
1 parent a3eec73 commit 72292f9
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 18 deletions.
Expand Up @@ -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>
Expand Down
22 changes: 22 additions & 0 deletions ios/sdk/WeexSDK/Sources/Component/WXListComponent.mm
Expand Up @@ -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
Expand Down
62 changes: 44 additions & 18 deletions ios/sdk/WeexSDK/Sources/Component/WXScrollerComponent.mm
Expand Up @@ -32,6 +32,7 @@
#import "WXComponent+Events.h"
#import "WXPageEventNotifyEvent.h"
#import "WXComponent+Layout.h"
#import "WXUtility.h"

@interface WXScrollerComponentView : UIScrollView
@end
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:)]) {
Expand Down Expand Up @@ -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
{
Expand Down
7 changes: 7 additions & 0 deletions ios/sdk/WeexSDK/Sources/Utility/WXUtility.h
Expand Up @@ -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
Expand Down

0 comments on commit 72292f9

Please sign in to comment.