Skip to content

Commit 0786941

Browse files
authored
fix(ios): fix onPageSelected event (#299)
1 parent 44ff683 commit 0786941

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

ios/ReactNativePageView.h

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

1212
@property(nonatomic) NSInteger initialPage;
13+
@property(nonatomic) NSInteger previousIndex;
1314
@property(nonatomic) NSInteger currentIndex;
1415
@property(nonatomic) NSInteger pageMargin;
1516
@property(nonatomic, readonly) BOOL scrollEnabled;

ios/ReactNativePageView.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher {
3636
if (self = [super init]) {
3737
_scrollEnabled = YES;
3838
_pageMargin = 0;
39+
_previousIndex = -1;
3940
_transitionStyle = UIPageViewControllerTransitionStyleScroll;
4041
_orientation = UIPageViewControllerNavigationOrientationHorizontal;
4142
_currentIndex = 0;
@@ -173,7 +174,10 @@ - (void)setReactViewControllers:(NSInteger)index
173174
weakSelf.currentView = controller.view;
174175

175176
if (weakSelf.eventDispatcher) {
176-
[weakSelf.eventDispatcher sendEvent:[[RCTOnPageSelected alloc] initWithReactTag:weakSelf.reactTag position:@(index) coalescingKey:coalescingKey]];
177+
if(_previousIndex != _currentIndex){
178+
[weakSelf.eventDispatcher sendEvent:[[RCTOnPageSelected alloc] initWithReactTag:weakSelf.reactTag position:@(index) coalescingKey:coalescingKey]];
179+
}
180+
_previousIndex = _currentIndex;
177181
}
178182

179183
}];
@@ -280,7 +284,7 @@ - (void)pageViewController:(UIPageViewController *)pageViewController
280284
NSUInteger currentIndex = [self.reactSubviews indexOfObject:currentVC.view];
281285

282286
self.currentIndex = currentIndex;
283-
287+
self.previousIndex = currentIndex - 1;
284288
self.currentView = currentVC.view;
285289
self.reactPageIndicatorView.currentPage = currentIndex;
286290

0 commit comments

Comments
 (0)