Skip to content

Commit

Permalink
fix(ios): Fix vertical list scroll directionin rtl
Browse files Browse the repository at this point in the history
  • Loading branch information
DrRefactor committed Oct 8, 2024
1 parent e1a677b commit c8c700d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
5 changes: 3 additions & 2 deletions ios/LEGACY/Fabric/LEGACY_RNCPagerViewComponentView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {

NSInteger position = self.currentIndex;

BOOL isHorizontalRtl = self.isHorizontal && ![self isLtrLayout];
BOOL isAnimatingBackwards = offset<0;

if (scrollView.isDragging) {
Expand All @@ -341,8 +342,8 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {

if (!_overdrag) {
NSInteger maxIndex = _nativeChildrenViewControllers.count - 1;
NSInteger firstPageIndex = [self isLtrLayout] ? 0 : maxIndex;
NSInteger lastPageIndex = [self isLtrLayout] ? maxIndex : 0;
NSInteger firstPageIndex = !isHorizontalRtl ? 0 : maxIndex;
NSInteger lastPageIndex = !isHorizontalRtl ? maxIndex : 0;
BOOL isFirstPage = _currentIndex == firstPageIndex;
BOOL isLastPage = _currentIndex == lastPageIndex;
CGFloat contentOffset =[self isHorizontal] ? scrollView.contentOffset.x : scrollView.contentOffset.y;
Expand Down
25 changes: 14 additions & 11 deletions ios/LEGACY/LEGACY_RNCPagerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,9 @@ - (void)goTo:(NSInteger)index animated:(BOOL)animated {
return;
}

BOOL isRTL = ![self isLtrLayout];

BOOL isForward = (index > self.currentIndex && !isRTL) || (index < self.currentIndex && isRTL);
BOOL isHorizontalRtl = [self isHorizontalRtlLayout];
BOOL isForward = isHorizontalRtl ? index < self.currentIndex : index > self.currentIndex;


UIPageViewControllerNavigationDirection direction = isForward ? UIPageViewControllerNavigationDirectionForward : UIPageViewControllerNavigationDirectionReverse;

long diff = labs(index - _currentIndex);
Expand Down Expand Up @@ -353,13 +351,13 @@ - (void)pageViewController:(UIPageViewController *)pageViewController

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerAfterViewController:(UIViewController *)viewController {
UIPageViewControllerNavigationDirection direction = [self isLtrLayout] ? UIPageViewControllerNavigationDirectionForward : UIPageViewControllerNavigationDirectionReverse;
UIPageViewControllerNavigationDirection direction = ![self isHorizontalRtlLayout] ? UIPageViewControllerNavigationDirectionForward : UIPageViewControllerNavigationDirectionReverse;
return [self nextControllerForController:viewController inDirection:direction];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerBeforeViewController:(UIViewController *)viewController {
UIPageViewControllerNavigationDirection direction = [self isLtrLayout] ? UIPageViewControllerNavigationDirectionReverse : UIPageViewControllerNavigationDirectionForward;
UIPageViewControllerNavigationDirection direction = ![self isHorizontalRtlLayout] ? UIPageViewControllerNavigationDirectionReverse : UIPageViewControllerNavigationDirectionForward;
return [self nextControllerForController:viewController inDirection:direction];
}

Expand All @@ -382,8 +380,8 @@ - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoi

if (!_overdrag) {
NSInteger maxIndex = self.reactSubviews.count - 1;
BOOL isFirstPage = [self isLtrLayout] ? _currentIndex == 0 : _currentIndex == maxIndex;
BOOL isLastPage = [self isLtrLayout] ? _currentIndex == maxIndex : _currentIndex == 0;
BOOL isFirstPage = ![self isHorizontalRtlLayout] ? _currentIndex == 0 : _currentIndex == maxIndex;
BOOL isLastPage = ![self isHorizontalRtlLayout] ? _currentIndex == maxIndex : _currentIndex == 0;
CGFloat contentOffset =[self isHorizontal] ? scrollView.contentOffset.x : scrollView.contentOffset.y;
CGFloat topBound = [self isHorizontal] ? scrollView.bounds.size.width : scrollView.bounds.size.height;

Expand Down Expand Up @@ -423,7 +421,8 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {

NSInteger position = self.currentIndex;

BOOL isAnimatingBackwards = ([self isLtrLayout] && offset<0) || (![self isLtrLayout] && offset > 0.05f);
BOOL isHorizontalRtl = [self isHorizontalRtlLayout];
BOOL isAnimatingBackwards = isHorizontalRtl ? offset > 0.05f : offset < 0;

if (scrollView.isDragging) {
_destinationIndex = isAnimatingBackwards ? _currentIndex - 1 : _currentIndex + 1;
Expand All @@ -436,8 +435,8 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {

if (!_overdrag) {
NSInteger maxIndex = self.reactSubviews.count - 1;
NSInteger firstPageIndex = [self isLtrLayout] ? 0 : maxIndex;
NSInteger lastPageIndex = [self isLtrLayout] ? maxIndex : 0;
NSInteger firstPageIndex = !isHorizontalRtl ? 0 : maxIndex;
NSInteger lastPageIndex = !isHorizontalRtl ? maxIndex : 0;
BOOL isFirstPage = _currentIndex == firstPageIndex;
BOOL isLastPage = _currentIndex == lastPageIndex;
CGFloat contentOffset =[self isHorizontal] ? scrollView.contentOffset.x : scrollView.contentOffset.y;
Expand Down Expand Up @@ -500,4 +499,8 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecogni
- (BOOL)isLtrLayout {
return [_layoutDirection isEqualToString:@"ltr"];
}

- (BOOL)isHorizontalRtlLayout {
return self.isHorizontal && ![self isLtrLayout];
}
@end

0 comments on commit c8c700d

Please sign in to comment.