Skip to content

ScrollView - only send the didScroll event if the document origin has changed #1838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions React/Views/ScrollView/RCTScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ - (void)setContentOffset:(CGPoint)contentOffset
#endif // macOS]
if (contentView && _centerContent && !CGSizeEqualToSize(contentView.frame.size, CGSizeZero)) {
CGSize subviewSize = contentView.frame.size;
#if TARGET_OS_OSX // [macOS
CGSize scrollViewSize = self.contentView.bounds.size;
#else // [macOS
#if !TARGET_OS_OSX // [macOS]
CGSize scrollViewSize = self.bounds.size;
#else // [macOS
CGSize scrollViewSize = self.contentView.bounds.size;
#endif // macOS]
if (subviewSize.width <= scrollViewSize.width) {
contentOffset.x = -(scrollViewSize.width - subviewSize.width) / 2.0;
Expand Down Expand Up @@ -381,6 +381,7 @@ @implementation RCTScrollView {
BOOL _allowNextScrollNoMatterWhat;
#if TARGET_OS_OSX // [macOS
BOOL _notifyDidScroll;
NSPoint _lastDocumentOrigin;
#endif // macOS]
CGRect _lastClippedToRect;
uint16_t _coalescingKey;
Expand Down Expand Up @@ -475,6 +476,7 @@ - (instancetype)initWithEventDispatcher:(id<RCTEventDispatcherProtocol>)eventDis
_scrollView.delaysContentTouches = NO;
#else // [macOS
_scrollView.postsBoundsChangedNotifications = YES;
_lastDocumentOrigin = NSZeroPoint;
#endif // macOS]

#if !TARGET_OS_OSX // [macOS]
Expand Down Expand Up @@ -874,8 +876,9 @@ - (void)scrollViewDocumentViewBoundsDidChange:(__unused NSNotification *)notific
_scrollView.contentOffset = _scrollView.contentOffset; // necessary for content centering when _centerContent == YES
}

// if scrollView is not ready, don't notify with scroll event
if (_notifyDidScroll) {
// only send didScroll event if scrollView is ready or document origin changed
BOOL didScroll = !NSEqualPoints(_scrollView.documentView.frame.origin, _lastDocumentOrigin);
if (_notifyDidScroll && didScroll) {
[self scrollViewDidScroll:_scrollView];
}
}
Expand Down Expand Up @@ -921,6 +924,9 @@ - (void)removeScrollListener:(NSObject<UIScrollViewDelegate> *)scrollListener

- (void)scrollViewDidScroll:(RCTCustomScrollView *)scrollView // [macOS]
{
#if TARGET_OS_OSX // [macOS
_lastDocumentOrigin = scrollView.documentView.frame.origin;
#endif // macOS]
NSTimeInterval now = CACurrentMediaTime();
[self updateClippedSubviews];
/**
Expand All @@ -941,9 +947,7 @@ - (void)scrollViewDidScroll:(RCTCustomScrollView *)scrollView // [macOS]
}
#if !TARGET_OS_OSX // [macOS]
RCT_FORWARD_SCROLL_EVENT(scrollViewDidScroll : scrollView);
#else // [macOS
(void) scrollView;
#endif // macOS]
#endif // [macOS]
}

#if !TARGET_OS_OSX // [macOS]
Expand Down