Skip to content

Commit bd17a7d

Browse files
authored
fix(ios): Crash when rapidly switching pages #510
1 parent 0213901 commit bd17a7d

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

ios/ReactNativePageView.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ NS_ASSUME_NONNULL_BEGIN
2424
@property(nonatomic) BOOL overdrag;
2525
@property(nonatomic) NSString* layoutDirection;
2626
@property(nonatomic) CGRect previousBounds;
27-
27+
@property(nonatomic, assign) BOOL animating;
2828

2929
- (void)goTo:(NSInteger)index animated:(BOOL)animated;
3030
- (void)shouldScroll:(BOOL)scrollEnabled;

ios/ReactNativePageView.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ @interface ReactNativePageView () <UIPageViewControllerDataSource, UIPageViewCon
1919
@property(nonatomic, weak) UIView *currentView;
2020

2121
@property(nonatomic, strong) NSHashTable<UIViewController *> *cachedControllers;
22-
@property (nonatomic, assign) CGPoint lastContentOffset;
22+
@property(nonatomic, assign) CGPoint lastContentOffset;
2323

2424
- (void)goTo:(NSInteger)index animated:(BOOL)animated;
2525
- (void)shouldScroll:(BOOL)scrollEnabled;
@@ -181,6 +181,10 @@ - (void)setReactViewControllers:(NSInteger)index
181181
__weak ReactNativePageView *weakSelf = self;
182182
uint16_t coalescingKey = _coalescingKey++;
183183

184+
if (animated == YES) {
185+
self.animating = YES;
186+
}
187+
184188
[self.reactPageViewController setViewControllers:@[controller]
185189
direction:direction
186190
animated:animated
@@ -189,6 +193,10 @@ - (void)setReactViewControllers:(NSInteger)index
189193
strongSelf.currentIndex = index;
190194
strongSelf.currentView = controller.view;
191195

196+
if (finished) {
197+
strongSelf.animating = NO;
198+
}
199+
192200
if (strongSelf.eventDispatcher) {
193201
if (strongSelf.lastReportedIndex != strongSelf.currentIndex) {
194202
if (shouldCallOnPageSelected) {

ios/ReactViewPagerManager.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ - (void) goToPage
3131
RCTLogError(@"Cannot find ReactNativePageView with tag #%@", reactTag);
3232
return;
3333
}
34-
[view goTo:index.integerValue animated:animated];
34+
if (!animated || !view.animating) {
35+
[view goTo:index.integerValue animated:animated];
36+
}
3537
}];
3638
}
3739

0 commit comments

Comments
 (0)