@@ -159,14 +159,16 @@ - (void)setupInitialController {
159159 [self setReactViewControllers: self .initialPage
160160 with: initialController
161161 direction: UIPageViewControllerNavigationDirectionForward
162- animated: YES ];
162+ animated: YES
163+ shouldCallOnPageSelected: YES ];
163164 }
164165}
165166
166167- (void )setReactViewControllers : (NSInteger )index
167168 with : (UIViewController *)controller
168169 direction : (UIPageViewControllerNavigationDirection)direction
169- animated : (BOOL )animated {
170+ animated : (BOOL )animated
171+ shouldCallOnPageSelected : (BOOL )shouldCallOnPageSelected {
170172 if (self.reactPageViewController == nil ) {
171173 return ;
172174 }
@@ -183,7 +185,9 @@ - (void)setReactViewControllers:(NSInteger)index
183185
184186 if (strongSelf.eventDispatcher ) {
185187 if (strongSelf.lastReportedIndex != strongSelf.currentIndex ) {
186- [strongSelf.eventDispatcher sendEvent: [[RCTOnPageSelected alloc ] initWithReactTag: strongSelf.reactTag position: @(index) coalescingKey: coalescingKey]];
188+ if (shouldCallOnPageSelected) {
189+ [strongSelf.eventDispatcher sendEvent: [[RCTOnPageSelected alloc ] initWithReactTag: strongSelf.reactTag position: @(index) coalescingKey: coalescingKey]];
190+ }
187191 strongSelf.lastReportedIndex = strongSelf.currentIndex ;
188192 }
189193 }
@@ -236,33 +240,39 @@ - (void)goTo:(NSInteger)index animated:(BOOL)animated {
236240 long diff = labs (index - _currentIndex);
237241
238242 if (isForward && diff > 0 ) {
239- for (NSInteger i=_currentIndex+1 ; i<=index; i++) {
240- [self goToViewController: i direction: direction animated: animated];
243+ for (NSInteger i=_currentIndex; i<=index; i++) {
244+ if (i == _currentIndex) {
245+ continue ;
246+ }
247+ [self goToViewController: i direction: direction animated: animated shouldCallOnPageSelected: i == index];
241248 }
242249 }
243250
244251 if (!isForward && diff > 0 ) {
245- for (NSInteger i=_currentIndex- 1 ; i>=index; i--) {
246- if (i >= 0 ) {
247- [ self goToViewController: i direction: direction animated: animated] ;
252+ for (NSInteger i=_currentIndex; i>=index; i--) {
253+ if (index == _currentIndex ) {
254+ continue ;
248255 }
256+ [self goToViewController: i direction: direction animated: animated shouldCallOnPageSelected: i == index];
249257 }
250258 }
251259
252260 if (diff == 0 ) {
253- [self goToViewController: index direction: direction animated: animated];
261+ [self goToViewController: index direction: direction animated: animated shouldCallOnPageSelected: YES ];
254262 }
255263}
256264
257265- (void )goToViewController : (NSInteger )index
258266 direction : (UIPageViewControllerNavigationDirection)direction
259- animated : (BOOL )animated {
267+ animated : (BOOL )animated
268+ shouldCallOnPageSelected : (BOOL )shouldCallOnPageSelected {
260269 UIView *viewToDisplay = self.reactSubviews [index];
261270 UIViewController *controllerToDisplay = [self findAndCacheControllerForView: viewToDisplay];
262271 [self setReactViewControllers: index
263272 with: controllerToDisplay
264273 direction: direction
265- animated: animated];
274+ animated: animated
275+ shouldCallOnPageSelected: shouldCallOnPageSelected];
266276}
267277
268278- (UIViewController *)findAndCacheControllerForView : (UIView *)viewToDisplay {
@@ -320,7 +330,6 @@ - (void)pageViewController:(UIPageViewController *)pageViewController
320330 self.currentIndex = currentIndex;
321331 self.currentView = currentVC.view ;
322332 self.reactPageIndicatorView .currentPage = currentIndex;
323-
324333 [self .eventDispatcher sendEvent: [[RCTOnPageSelected alloc ] initWithReactTag: self .reactTag position: @(currentIndex) coalescingKey: _coalescingKey++]];
325334 [self .eventDispatcher sendEvent: [[RCTOnPageScrollEvent alloc ] initWithReactTag: self .reactTag position: @(currentIndex) offset: @(0.0 )]];
326335 self.lastReportedIndex = currentIndex;
0 commit comments