Skip to content

Commit a26869c

Browse files
authored
fix(#722): revert not working back navigation gesture in favour of PagerView inside ScrollView (#933)
1 parent 386a44c commit a26869c

File tree

4 files changed

+59
-61
lines changed

4 files changed

+59
-61
lines changed

example/src/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import TabBarIconExample from './tabView/TabBarIconExample';
3030
import CustomIndicatorExample from './tabView/CustomIndicatorExample';
3131
import CustomTabBarExample from './tabView/CustomTabBarExample';
3232
import CoverflowExample from './tabView/CoverflowExample';
33+
import { TabViewInsideScrollViewExample } from './tabView/TabViewInsideScrollViewExample';
3334
import ReanimatedOnPageScrollExample from './ReanimatedOnPageScrollExample';
3435
import { MaterialTopBarExample } from './MaterialTopTabExample';
3536
import { createNativeStackNavigator } from '@react-navigation/native-stack';
@@ -49,6 +50,7 @@ const examples = [
4950
component: ScrollablePagerViewExample,
5051
name: 'Scrollable PagerView Example',
5152
},
53+
{ component: TabViewInsideScrollViewExample, name: 'TabView inside ScrollView Example' },
5254
{
5355
component: ScrollViewInsideExample,
5456
name: 'ScrollView inside PagerView Example',
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import * as React from 'react';
2+
import { View, useWindowDimensions, Text, ScrollView } from 'react-native';
3+
import { TabView, SceneMap } from 'react-native-tab-view';
4+
import { Header } from 'react-native/Libraries/NewAppScreen';
5+
6+
function FirstRoute() {
7+
return (
8+
<View style={{ flex: 1, padding: 20, backgroundColor: 'blue' }}>
9+
<Text style={{color: 'white'}}>First Route</Text>
10+
</View>
11+
);
12+
}
13+
14+
function SecondRoute() {
15+
return (
16+
<View style={{ flex: 1, padding: 20, backgroundColor: 'purple' }}>
17+
<Text style={{color: 'white'}}>Second Route</Text>
18+
</View>
19+
);
20+
}
21+
22+
const renderScene = SceneMap({
23+
first: FirstRoute,
24+
second: SecondRoute,
25+
});
26+
27+
const routes = [
28+
{ key: 'first', title: 'First' },
29+
{ key: 'second', title: 'Second' },
30+
];
31+
32+
export function TabViewInsideScrollViewExample() {
33+
const layout = useWindowDimensions();
34+
const [index, setIndex] = React.useState(0);
35+
36+
return (
37+
<ScrollView
38+
contentContainerStyle={{flexGrow: 1, backgroundColor: 'red'}}
39+
nestedScrollEnabled={false}
40+
scrollEnabled={true}
41+
>
42+
<Header />
43+
44+
<View>
45+
<TabView
46+
style={{height: 1200}}
47+
navigationState={{ index, routes }}
48+
renderScene={renderScene}
49+
onIndexChange={setIndex}
50+
initialLayout={{ width: layout.width }}
51+
/>
52+
</View>
53+
</ScrollView>
54+
);
55+
}

ios/Fabric/RNCPagerViewComponentView.mm

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414

1515
using namespace facebook::react;
1616

17-
@interface RNCPagerViewComponentView () <RCTRNCViewPagerViewProtocol, UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate, UIGestureRecognizerDelegate>
18-
19-
@property(nonatomic, assign) UIPanGestureRecognizer* panGestureRecognizer;
17+
@interface RNCPagerViewComponentView () <RCTRNCViewPagerViewProtocol, UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate>
2018

2119
@end
2220

@@ -73,11 +71,6 @@ - (instancetype)initWithFrame:(CGRect)frame
7371
_destinationIndex = -1;
7472
_layoutDirection = @"ltr";
7573
_overdrag = NO;
76-
UIPanGestureRecognizer* panGestureRecognizer = [UIPanGestureRecognizer new];
77-
self.panGestureRecognizer = panGestureRecognizer;
78-
panGestureRecognizer.delegate = self;
79-
[self addGestureRecognizer: panGestureRecognizer];
80-
8174
}
8275

8376
return self;
@@ -420,30 +413,6 @@ + (ComponentDescriptorProvider)componentDescriptorProvider
420413
return concreteComponentDescriptorProvider<RNCViewPagerComponentDescriptor>();
421414
}
422415

423-
424-
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
425-
426-
// Recognize simultaneously only if the other gesture is RN Screen's pan gesture (one that is used to perform fullScreenGestureEnabled)
427-
if (gestureRecognizer == self.panGestureRecognizer && [NSStringFromClass([otherGestureRecognizer class]) isEqual: @"RNSPanGestureRecognizer"]) {
428-
UIPanGestureRecognizer* panGestureRecognizer = (UIPanGestureRecognizer*) gestureRecognizer;
429-
CGPoint velocity = [panGestureRecognizer velocityInView:self];
430-
BOOL isLTR = [self isLtrLayout];
431-
BOOL isBackGesture = (isLTR && velocity.x > 0) || (!isLTR && velocity.x < 0);
432-
433-
if (self.currentIndex == 0 && isBackGesture) {
434-
scrollView.panGestureRecognizer.enabled = false;
435-
} else {
436-
const auto &viewProps = *std::static_pointer_cast<const RNCViewPagerProps>(_props);
437-
scrollView.panGestureRecognizer.enabled = viewProps.scrollEnabled;
438-
}
439-
440-
return YES;
441-
}
442-
const auto &viewProps = *std::static_pointer_cast<const RNCViewPagerProps>(_props);
443-
scrollView.panGestureRecognizer.enabled = viewProps.scrollEnabled;
444-
return NO;
445-
}
446-
447416
@end
448417

449418
Class<RCTComponentViewProtocol> RNCViewPagerCls(void)

ios/RNCPagerView.m

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
#import "RCTOnPageSelected.h"
1010
#import <math.h>
1111

12-
@interface RNCPagerView () <UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate, UIGestureRecognizerDelegate>
13-
14-
@property(nonatomic, assign) UIPanGestureRecognizer* panGestureRecognizer;
12+
@interface RNCPagerView () <UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate>
1513

1614
@property(nonatomic, strong) UIPageViewController *reactPageViewController;
1715
@property(nonatomic, strong) id<RCTEventDispatcherProtocol> eventDispatcher;
@@ -51,10 +49,6 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge {
5149
_cachedControllers = [NSHashTable hashTableWithOptions:NSHashTableStrongMemory];
5250
_overdrag = NO;
5351
_layoutDirection = @"ltr";
54-
UIPanGestureRecognizer* panGestureRecognizer = [UIPanGestureRecognizer new];
55-
self.panGestureRecognizer = panGestureRecognizer;
56-
panGestureRecognizer.delegate = self;
57-
[self addGestureRecognizer: panGestureRecognizer];
5852
}
5953
return self;
6054
}
@@ -480,28 +474,6 @@ - (NSString *)determineScrollDirection:(UIScrollView *)scrollView {
480474
return scrollDirection;
481475
}
482476

483-
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
484-
485-
// Recognize simultaneously only if the other gesture is RN Screen's pan gesture (one that is used to perform fullScreenGestureEnabled)
486-
if (gestureRecognizer == self.panGestureRecognizer && [NSStringFromClass([otherGestureRecognizer class]) isEqual: @"RNSPanGestureRecognizer"]) {
487-
UIPanGestureRecognizer* panGestureRecognizer = (UIPanGestureRecognizer*) gestureRecognizer;
488-
CGPoint velocity = [panGestureRecognizer velocityInView:self];
489-
BOOL isLTR = [self isLtrLayout];
490-
BOOL isBackGesture = (isLTR && velocity.x > 0) || (!isLTR && velocity.x < 0);
491-
492-
if (self.currentIndex == 0 && isBackGesture) {
493-
self.scrollView.panGestureRecognizer.enabled = false;
494-
} else {
495-
self.scrollView.panGestureRecognizer.enabled = self.scrollEnabled;
496-
}
497-
498-
return YES;
499-
}
500-
501-
self.scrollView.panGestureRecognizer.enabled = self.scrollEnabled;
502-
return NO;
503-
}
504-
505477
- (BOOL)isLtrLayout {
506478
return [_layoutDirection isEqualToString:@"ltr"];
507479
}

0 commit comments

Comments
 (0)