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

Merged
merged 2 commits into from
Oct 28, 2024
Merged
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
3 changes: 2 additions & 1 deletion ios/LEGACY/Fabric/LEGACY_RNCPagerViewComponentView.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
#import <Foundation/Foundation.h>
#import <React/RCTViewComponentView.h>
#import "UIViewController+CreateExtension.h"
#import "UIView+isHorizontalRtlLayout.h"

NS_ASSUME_NONNULL_BEGIN

@interface LEGACY_RNCPagerViewComponentView : RCTViewComponentView <UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate>
@interface LEGACY_RNCPagerViewComponentView : RCTViewComponentView <UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate, isHorizontalRtlLayout>

@property(strong, nonatomic, readonly) UIPageViewController *nativePageViewController;
@property(nonatomic, strong) NSMutableArray<UIViewController *> *nativeChildrenViewControllers;
Expand Down
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 isHorizontalRtlLayout];
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
3 changes: 2 additions & 1 deletion ios/LEGACY/LEGACY_RNCPagerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#import <React/RCTShadowView.h>
#import <React/UIView+React.h>
#import <UIKit/UIKit.h>
#import "UIView+isHorizontalRtlLayout.h"

NS_ASSUME_NONNULL_BEGIN

@interface LEGACY_RNCPagerView: UIView
@interface LEGACY_RNCPagerView: UIView <RtlLayoutProtocol>

- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher;

Expand Down
22 changes: 11 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,5 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecogni
- (BOOL)isLtrLayout {
return [_layoutDirection isEqualToString:@"ltr"];
}

@end
14 changes: 14 additions & 0 deletions ios/UIView+isHorizontalRtlLayout.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#import <UIKit/UIKit.h>

@protocol RtlLayoutProtocol <NSObject>

@property (nonatomic, readonly) BOOL isHorizontal;
@property (nonatomic, readonly) BOOL isLtrLayout;

@end

@interface UIView (RtlLayoutProtocol)

- (BOOL)isHorizontalRtlLayout;

@end
14 changes: 14 additions & 0 deletions ios/UIView+isHorizontalRtlLayout.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#import "UIView+isHorizontalRtlLayout.h"


@implementation UIView (RtlLayoutProtocol)

- (BOOL)isHorizontalRtlLayout {
if ([self conformsToProtocol:@protocol(RtlLayoutProtocol)]) {
id<RtlLayoutProtocol> layoutObject = (id<RtlLayoutProtocol>)self;
return layoutObject.isHorizontal && !layoutObject.isLtrLayout;
}
return NO;
}

@end
Loading