Skip to content

Commit 93182a5

Browse files
authored
fix(ios): onPageSelected is not called (#334)
1 parent 6ba8356 commit 93182a5

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

ios/ReactNativePageView.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ NS_ASSUME_NONNULL_BEGIN
1010
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher;
1111

1212
@property(nonatomic) NSInteger initialPage;
13-
@property(nonatomic) NSInteger previousIndex;
13+
@property(nonatomic) NSInteger lastReportedIndex;
1414
@property(nonatomic) NSInteger currentIndex;
1515
@property(nonatomic) NSInteger pageMargin;
1616
@property(nonatomic, readonly) BOOL scrollEnabled;

ios/ReactNativePageView.m

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher {
3636
if (self = [super init]) {
3737
_scrollEnabled = YES;
3838
_pageMargin = 0;
39-
_previousIndex = -1;
39+
_lastReportedIndex = -1;
4040
_transitionStyle = UIPageViewControllerTransitionStyleScroll;
4141
_orientation = UIPageViewControllerNavigationOrientationHorizontal;
4242
_currentIndex = 0;
@@ -169,17 +169,16 @@ - (void)setReactViewControllers:(NSInteger)index
169169
direction:direction
170170
animated:animated
171171
completion:^(BOOL finished) {
172+
__strong typeof(self) strongSelf = weakSelf;
173+
strongSelf.currentIndex = index;
174+
strongSelf.currentView = controller.view;
172175

173-
weakSelf.currentIndex = index;
174-
weakSelf.currentView = controller.view;
175-
176-
if (weakSelf.eventDispatcher) {
177-
if(_previousIndex != _currentIndex){
178-
[weakSelf.eventDispatcher sendEvent:[[RCTOnPageSelected alloc] initWithReactTag:weakSelf.reactTag position:@(index) coalescingKey:coalescingKey]];
176+
if (strongSelf.eventDispatcher) {
177+
if (strongSelf.lastReportedIndex != strongSelf.currentIndex) {
178+
[strongSelf.eventDispatcher sendEvent:[[RCTOnPageSelected alloc] initWithReactTag:strongSelf.reactTag position:@(index) coalescingKey:coalescingKey]];
179+
strongSelf.lastReportedIndex = strongSelf.currentIndex;
179180
}
180-
_previousIndex = _currentIndex;
181181
}
182-
183182
}];
184183
}
185184

@@ -284,12 +283,12 @@ - (void)pageViewController:(UIPageViewController *)pageViewController
284283
NSUInteger currentIndex = [self.reactSubviews indexOfObject:currentVC.view];
285284

286285
self.currentIndex = currentIndex;
287-
self.previousIndex = currentIndex - 1;
288286
self.currentView = currentVC.view;
289287
self.reactPageIndicatorView.currentPage = currentIndex;
290288

291289
[self.eventDispatcher sendEvent:[[RCTOnPageSelected alloc] initWithReactTag:self.reactTag position:@(currentIndex) coalescingKey:_coalescingKey++]];
292290
[self.eventDispatcher sendEvent:[[RCTOnPageScrollEvent alloc] initWithReactTag:self.reactTag position:@(currentIndex) offset:@(0.0)]];
291+
self.lastReportedIndex = currentIndex;
293292
}
294293
}
295294

0 commit comments

Comments
 (0)