Skip to content
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

fix(ios): Fix vertical list scroll directionin rtl #885

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions ios/LEGACY/Fabric/LEGACY_RNCPagerViewComponentView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ - (BOOL)isLtrLayout {
return [_layoutDirection isEqualToString: @"ltr"];
}

- (BOOL)isHorizontalRtlLayout {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you extract this function to a separate file? It is used in two places.

return self.isHorizontal && ![self isLtrLayout];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint point = scrollView.contentOffset;

Expand All @@ -328,6 +332,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {

NSInteger position = self.currentIndex;

BOOL isHorizontalRtl = [self isHorizontalRtlLayout];
BOOL isAnimatingBackwards = offset<0;

if (scrollView.isDragging) {
Expand All @@ -341,8 +346,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