Skip to content

Commit 5d68632

Browse files
authored
fix(ios): overdrag with vertical scroll (#390) [#388]
1 parent 2708767 commit 5d68632

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

ios/ReactNativePageView.m

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,14 @@ - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoi
342342
[self.eventDispatcher sendEvent:[[RCTOnPageScrollStateChanged alloc] initWithReactTag:self.reactTag state:@"settling" coalescingKey:_coalescingKey++]];
343343

344344
if (!_overdrag) {
345-
if (_currentIndex == 0 && scrollView.contentOffset.x <= scrollView.bounds.size.width) {
346-
*targetContentOffset = CGPointMake(scrollView.bounds.size.width, 0);
347-
} else if (_currentIndex == _reactPageIndicatorView.numberOfPages -1 && scrollView.contentOffset.x >= scrollView.bounds.size.width) {
348-
*targetContentOffset = CGPointMake(scrollView.bounds.size.width, 0);
345+
BOOL isFirstPage = _currentIndex == 0;
346+
BOOL isLastPage = _currentIndex == _reactPageIndicatorView.numberOfPages - 1;
347+
CGFloat contentOffset =[self isHorizontal] ? scrollView.contentOffset.x : scrollView.contentOffset.y;
348+
CGFloat topBound = [self isHorizontal] ? scrollView.bounds.size.width : scrollView.bounds.size.height;
349+
350+
if ((isFirstPage && contentOffset <= topBound) || (isLastPage && contentOffset >= topBound)) {
351+
CGPoint croppedOffset = [self isHorizontal] ? CGPointMake(topBound, 0) : CGPointMake(0, topBound);
352+
*targetContentOffset = croppedOffset;
349353
}
350354
}
351355
}
@@ -383,11 +387,14 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
383387
}
384388

385389
if (!_overdrag) {
386-
if (_currentIndex == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width) {
387-
scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0);
388-
absoluteOffset=0;
389-
} else if (_currentIndex == _reactPageIndicatorView.numberOfPages - 1 && scrollView.contentOffset.x > scrollView.bounds.size.width) {
390-
scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0);
390+
BOOL isFirstPage = _currentIndex == 0;
391+
BOOL isLastPage = _currentIndex == _reactPageIndicatorView.numberOfPages - 1;
392+
CGFloat contentOffset =[self isHorizontal] ? scrollView.contentOffset.x : scrollView.contentOffset.y;
393+
CGFloat topBound = [self isHorizontal] ? scrollView.bounds.size.width : scrollView.bounds.size.height;
394+
395+
if ((isFirstPage && contentOffset <= topBound) || (isLastPage && contentOffset >= topBound)) {
396+
CGPoint croppedOffset = [self isHorizontal] ? CGPointMake(topBound, 0) : CGPointMake(0, topBound);
397+
scrollView.contentOffset = croppedOffset;
391398
absoluteOffset=0;
392399
}
393400
}

0 commit comments

Comments
 (0)