Skip to content

Commit

Permalink
fix(#722): revert not working back navigation gesture in favour of Pa…
Browse files Browse the repository at this point in the history
…gerView inside ScrollView
  • Loading branch information
MateWW committed Nov 21, 2024
1 parent 88f2361 commit 41a7e80
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 61 deletions.
2 changes: 2 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { NextBasicPagerViewExample } from './NextBasicPagerViewExample';
import { PagerHookExample } from './PagerHookExample';
import { NestedTabViewExample } from './tabView/NestedTabViewExample';

const examples = [
{ component: NestedTabViewExample, name: 'Nested TabView Example' },
{ component: BasicPagerViewExample, name: 'Basic Example' },
{ component: PagerHookExample, name: 'Pager Hook Example' },
{ component: KeyboardExample, name: 'Keyboard Example' },
Expand Down
67 changes: 67 additions & 0 deletions example/src/tabView/NestedTabViewExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import * as React from 'react';
import { View, useWindowDimensions, Text, ScrollView } from 'react-native';
import { TabView, SceneMap } from 'react-native-tab-view';
import {Header} from 'react-native/Libraries/NewAppScreen';

function FirstRoute() {
return (
<View style={{ flex: 1, padding: 20, backgroundColor: 'blue' }}>
<Text style={{color: 'white'}}>First Route</Text>
</View>
);
}

function SecondRoute() {
return (
<View style={{ flex: 1, padding: 20, backgroundColor: 'purple' }}>
<Text style={{color: 'white'}}>Second Route</Text>
</View>
);
}

const renderScene = SceneMap({
first: FirstRoute,
second: SecondRoute,
});

const routes = [
{ key: 'first', title: 'First' },
{ key: 'second', title: 'Second' },
];

function Xd() {
return <ScrollView contentContainerStyle={{flexGrow: 1, backgroundColor: 'red', height: 5000}} nestedScrollEnabled>
<ScrollView
contentContainerStyle={{flexGrow: 1, backgroundColor: 'blue', width: 1000, height: 300}}
horizontal={true}
>
<Text>Test horizontal</Text>
</ScrollView>
</ScrollView>
}

export function NestedTabViewExample() {
const layout = useWindowDimensions();
const [index, setIndex] = React.useState(0);

return (
<ScrollView
contentContainerStyle={{flexGrow: 1, backgroundColor: 'red'}}
nestedScrollEnabled={false}
scrollEnabled={true}
>
<Header />

<View>
<TabView
// TODO: Fix the height measurement for nested pager view
style={{height: 5000}}
navigationState={{ index, routes }}
renderScene={renderScene}
onIndexChange={setIndex}
initialLayout={{ width: layout.width }}
/>
</View>
</ScrollView>
);
}
33 changes: 1 addition & 32 deletions ios/LEGACY/Fabric/LEGACY_RNCPagerViewComponentView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

using namespace facebook::react;

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

@property(nonatomic, assign) UIPanGestureRecognizer* panGestureRecognizer;
@interface LEGACY_RNCPagerViewComponentView () <RCTLEGACY_RNCViewPagerViewProtocol, UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate>

@end

Expand Down Expand Up @@ -71,11 +69,6 @@ - (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 Down Expand Up @@ -408,30 +401,6 @@ + (ComponentDescriptorProvider)componentDescriptorProvider
return concreteComponentDescriptorProvider<LEGACY_RNCViewPagerComponentDescriptor>();
}


- (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)
Expand Down
30 changes: 1 addition & 29 deletions ios/LEGACY/LEGACY_RNCPagerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
#import "RCTOnPageSelected.h"
#import <math.h>

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

@property(nonatomic, assign) UIPanGestureRecognizer* panGestureRecognizer;
@interface LEGACY_RNCPagerView () <UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate>

@property(nonatomic, strong) UIPageViewController *reactPageViewController;
@property(nonatomic, strong) RCTEventDispatcher *eventDispatcher;
Expand Down Expand Up @@ -48,10 +46,6 @@ - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher {
_cachedControllers = [NSHashTable hashTableWithOptions:NSHashTableStrongMemory];
_overdrag = NO;
_layoutDirection = @"ltr";
UIPanGestureRecognizer* panGestureRecognizer = [UIPanGestureRecognizer new];
self.panGestureRecognizer = panGestureRecognizer;
panGestureRecognizer.delegate = self;
[self addGestureRecognizer: panGestureRecognizer];
}
return self;
}
Expand Down Expand Up @@ -474,28 +468,6 @@ - (NSString *)determineScrollDirection:(UIScrollView *)scrollView {
return scrollDirection;
}

- (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) {
self.scrollView.panGestureRecognizer.enabled = false;
} else {
self.scrollView.panGestureRecognizer.enabled = self.scrollEnabled;
}

return YES;
}

self.scrollView.panGestureRecognizer.enabled = self.scrollEnabled;
return NO;
}

- (BOOL)isLtrLayout {
return [_layoutDirection isEqualToString:@"ltr"];
}
Expand Down

0 comments on commit 41a7e80

Please sign in to comment.