Skip to content

Commit

Permalink
fix iOS issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Trocki committed Mar 29, 2024
1 parent 1518c40 commit 9f0be55
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 1,101 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ lefthook.yml
# testing
/coverage
.cxx
example/ios/PagerViewExample.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
5 changes: 3 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ PODS:
- React-jsinspector (0.72.6)
- React-logger (0.72.6):
- glog
- react-native-pager-view (6.2.1):
- react-native-pager-view (6.3.0):
- RCT-Folly (= 2021.07.22.00)
- React-Core
- react-native-safe-area-context (4.5.2):
- RCT-Folly
Expand Down Expand Up @@ -754,7 +755,7 @@ SPEC CHECKSUMS:
React-jsiexecutor: 3bf18ff7cb03cd8dfdce08fbbc0d15058c1d71ae
React-jsinspector: 194e32c6aab382d88713ad3dd0025c5f5c4ee072
React-logger: cebf22b6cf43434e471dc561e5911b40ac01d289
react-native-pager-view: d211379f61895b6349bd7e571b44a26d005c2975
react-native-pager-view: 40d9ec4a63ed74f8aa2f1e1bba7c1e0b4080d8a6
react-native-safe-area-context: 1d596539b05a78f2b346e954e7c577f6f9be7544
React-NativeModulesApple: 02e35e9a51e10c6422f04f5e4076a7c02243fff2
React-perflogger: e3596db7e753f51766bceadc061936ef1472edc3
Expand Down
7 changes: 6 additions & 1 deletion example/src/ScrollViewInsideExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ export const ScrollViewInsideExample = (): JSX.Element => {
);

return (
<AnimatedPagerView testID="pager-view" style={styles.flex}>
<AnimatedPagerView
// todo fix this case
useLegacy
testID="pager-view"
style={styles.flex}
>
{pages.map((page) => (
<ScrollView key={page.key} style={styles.content}>
{Array(20)
Expand Down
38 changes: 24 additions & 14 deletions example/src/ScrollablePagerViewExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,30 @@ export function ScrollablePagerViewExample() {
}}
>
<ScrollView testID={'scroll-view'} style={styles.flex}>
{navigationPanel.pages.map(({ key }) => (
<AnimatedPagerView
{...navigationPanel}
testID={'pager-view'}
ref={ref}
key={key}
style={{ height: HEIGHT }}
>
{navigationPanel.pages.map((page) => (
<View key={`${key}+${page.key}`} style={styles.content}>
<Image style={styles.flex} source={page.imgSource} />
</View>
))}
</AnimatedPagerView>
<AnimatedPagerView
{...navigationPanel}
testID={'pager-view'}
ref={ref}
// todo fix it
useLegacy
style={{ height: HEIGHT }}
>
{navigationPanel.pages.map((page) => (
<View key={`${page.key}`} style={styles.content}>
<Image style={styles.flex} source={page.imgSource} />
</View>
))}
</AnimatedPagerView>
{new Array(10).fill(1).map((_, index) => (
<View
key={index}
style={{
width: '100%',
height: 200,
backgroundColor: 'purple',
marginBottom: 10,
}}
/>
))}
</ScrollView>
<NavigationPanel {...navigationPanel} />
Expand Down
46 changes: 42 additions & 4 deletions ios/LEGACY/Fabric/LEGACY_RNCPagerViewComponentView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

using namespace facebook::react;

@interface LEGACY_RNCPagerViewComponentView () <RCTLEGACY_RNCViewPagerViewProtocol, UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate>
@interface LEGACY_RNCPagerViewComponentView () <RCTLEGACY_RNCViewPagerViewProtocol, UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate, UIGestureRecognizerDelegate>

@property(nonatomic, assign) UIPanGestureRecognizer* panGestureRecognizer;

@end

@implementation LEGACY_RNCPagerViewComponentView {
Expand Down Expand Up @@ -63,6 +66,11 @@ - (instancetype)initWithFrame:(CGRect)frame
_destinationIndex = -1;
_layoutDirection = @"ltr";
_overdrag = NO;
UIPanGestureRecognizer* panGestureRecognizer = [UIPanGestureRecognizer new];
self.panGestureRecognizer = panGestureRecognizer;
panGestureRecognizer.delegate = self;
[self addGestureRecognizer: panGestureRecognizer];

}

return self;
Expand All @@ -75,6 +83,7 @@ - (void)willMoveToSuperview:(UIView *)newSuperview {
}
}


#pragma mark - React API

- (void)mountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index {
Expand Down Expand Up @@ -113,6 +122,7 @@ -(void)prepareForRecycle {
}

- (void)shouldDismissKeyboard:(LEGACY_RNCViewPagerKeyboardDismissMode)dismissKeyboard {
#if !TARGET_OS_VISION
UIScrollViewKeyboardDismissMode dismissKeyboardMode = UIScrollViewKeyboardDismissModeNone;
switch (dismissKeyboard) {
case LEGACY_RNCViewPagerKeyboardDismissMode::None:
Expand All @@ -123,6 +133,7 @@ - (void)shouldDismissKeyboard:(LEGACY_RNCViewPagerKeyboardDismissMode)dismissKey
break;
}
scrollView.keyboardDismissMode = dismissKeyboardMode;
#endif
}


Expand Down Expand Up @@ -259,6 +270,9 @@ - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {

const auto strongEventEmitter = *std::dynamic_pointer_cast<const LEGACY_RNCViewPagerEventEmitter>(_eventEmitter);
strongEventEmitter.onPageScrollStateChanged(LEGACY_RNCViewPagerEventEmitter::OnPageScrollStateChanged{.pageScrollState = LEGACY_RNCViewPagerEventEmitter::OnPageScrollStateChangedPageScrollState::Settling });

if (!_overdrag) {
NSInteger maxIndex = _nativeChildrenViewControllers.count - 1;
BOOL isFirstPage = [self isLtrLayout] ? _currentIndex == 0 : _currentIndex == maxIndex;
Expand All @@ -269,11 +283,12 @@ - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoi
if ((isFirstPage && contentOffset <= topBound) || (isLastPage && contentOffset >= topBound)) {
CGPoint croppedOffset = [self isHorizontal] ? CGPointMake(topBound, 0) : CGPointMake(0, topBound);
*targetContentOffset = croppedOffset;

strongEventEmitter.onPageScrollStateChanged(LEGACY_RNCViewPagerEventEmitter::OnPageScrollStateChanged{.pageScrollState = LEGACY_RNCViewPagerEventEmitter::OnPageScrollStateChangedPageScrollState::Idle });
}
}

const auto strongEventEmitter = *std::dynamic_pointer_cast<const LEGACY_RNCViewPagerEventEmitter>(_eventEmitter);
strongEventEmitter.onPageScrollStateChanged(LEGACY_RNCViewPagerEventEmitter::OnPageScrollStateChanged{.pageScrollState = LEGACY_RNCViewPagerEventEmitter::OnPageScrollStateChangedPageScrollState::Settling });

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
Expand Down Expand Up @@ -393,11 +408,34 @@ + (ComponentDescriptorProvider)componentDescriptorProvider
}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

// Recognize simultaneously only if the other gesture is RN Screen's pan gesture (one that is used to perform fullScreenGestureEnabled)
if (gestureRecognizer == self.panGestureRecognizer && [NSStringFromClass([otherGestureRecognizer class]) isEqual: @"RNSPanGestureRecognizer"]) {
UIPanGestureRecognizer* panGestureRecognizer = (UIPanGestureRecognizer*) gestureRecognizer;
CGPoint velocity = [panGestureRecognizer velocityInView:self];
BOOL isLTR = [self isLtrLayout];
BOOL isBackGesture = (isLTR && velocity.x > 0) || (!isLTR && velocity.x < 0);

if (self.currentIndex == 0 && isBackGesture) {
scrollView.panGestureRecognizer.enabled = false;
} else {
const auto &viewProps = *std::static_pointer_cast<const LEGACY_RNCViewPagerProps>(_props);
scrollView.panGestureRecognizer.enabled = viewProps.scrollEnabled;
}

return YES;
}
const auto &viewProps = *std::static_pointer_cast<const LEGACY_RNCViewPagerProps>(_props);
scrollView.panGestureRecognizer.enabled = viewProps.scrollEnabled;
return NO;
}

@end

Class<RCTComponentViewProtocol> LEGACY_RNCViewPagerCls(void)
{
return LEGACY_RNCPagerViewComponentView.class;
}

#endif
#endif
5 changes: 3 additions & 2 deletions ios/LEGACY/LEGACY_RNCPagerView.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#import "LEGACY_RNCPagerView.h"
#import "React/RCTLog.h"
#import <React/RCTViewManager.h>
Expand All @@ -9,7 +8,9 @@
#import "RCTOnPageSelected.h"
#import <math.h>

@interface LEGACY_RNCPagerView () <UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate>
@interface LEGACY_RNCPagerView () <UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate, UIGestureRecognizerDelegate>

@property(nonatomic, assign) UIPanGestureRecognizer* panGestureRecognizer;

@property(nonatomic, strong) UIPageViewController *reactPageViewController;
@property(nonatomic, strong) RCTEventDispatcher *eventDispatcher;
Expand Down
Loading

0 comments on commit 9f0be55

Please sign in to comment.