Skip to content

Commit 80860dd

Browse files
authored
fix(ios): onPageScroll event offset value (#293)
* fix(ios): onPageScroll event offset value * fix(ios): adjust scrolling logic to android values
1 parent c9fffc7 commit 80860dd

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

ios/ReactNativePageView.m

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ @interface ReactNativePageView () <UIPageViewControllerDataSource, UIPageViewCon
1818
@property(nonatomic, weak) UIView *currentView;
1919

2020
@property(nonatomic, strong) NSHashTable<UIViewController *> *cachedControllers;
21+
@property (nonatomic, assign) CGPoint lastContentOffset;
2122

2223
- (void)goTo:(NSInteger)index animated:(BOOL)animated;
2324
- (void)shouldScroll:(BOOL)scrollEnabled;
2425
- (void)shouldShowPageIndicator:(BOOL)showPageIndicator;
2526
- (void)shouldDismissKeyboard:(NSString *)dismissKeyboard;
2627

28+
2729
@end
2830

2931
@implementation ReactNativePageView {
@@ -349,8 +351,13 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
349351
[self.eventDispatcher sendEvent:[[RCTOnPageScrollStateChanged alloc] initWithReactTag:self.reactTag state:@"idle" coalescingKey:_coalescingKey++]];
350352
}
351353

354+
- (BOOL)isHorizontal {
355+
return self.orientation == UIPageViewControllerNavigationOrientationHorizontal;
356+
}
357+
352358
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
353359
CGPoint point = scrollView.contentOffset;
360+
354361
float offset = 0;
355362

356363
if (!_overdrag) {
@@ -361,7 +368,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
361368
}
362369
}
363370

364-
if (self.orientation == UIPageViewControllerNavigationOrientationHorizontal) {
371+
if (self.isHorizontal) {
365372
if (self.frame.size.width != 0) {
366373
offset = (point.x - self.frame.size.width)/self.frame.size.width;
367374
}
@@ -370,10 +377,41 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
370377
offset = (point.y - self.frame.size.height)/self.frame.size.height;
371378
}
372379
}
373-
if(fabs(offset) > 1) {
374-
offset = offset > 0 ? 1.0 : -1.0;
380+
381+
float absoluteOffset = fabs(offset);
382+
if(absoluteOffset > 1) {
383+
absoluteOffset = 1.0;
384+
}
385+
386+
NSString *scrollDirection = [self determineScrollDirection:scrollView];
387+
NSString *oppositeDirection = self.isHorizontal ? @"left" : @"up";
388+
NSInteger position = self.currentIndex;
389+
390+
if(absoluteOffset > 0) {
391+
position = [scrollDirection isEqual: oppositeDirection] ? self.currentIndex - 1 : self.currentIndex;
392+
absoluteOffset = [scrollDirection isEqual: oppositeDirection] ? 1 - absoluteOffset : absoluteOffset;
375393
}
376-
[self.eventDispatcher sendEvent:[[RCTOnPageScrollEvent alloc] initWithReactTag:self.reactTag position:@(self.currentIndex) offset:@(offset)]];
394+
395+
396+
self.lastContentOffset = scrollView.contentOffset;
397+
[self.eventDispatcher sendEvent:[[RCTOnPageScrollEvent alloc] initWithReactTag:self.reactTag position:@(position) offset:@(absoluteOffset)]];
377398
}
378399

400+
- (NSString *)determineScrollDirection:(UIScrollView *)scrollView {
401+
NSString *scrollDirection;
402+
if (self.isHorizontal) {
403+
if (self.lastContentOffset.x > scrollView.contentOffset.x) {
404+
scrollDirection = @"left";
405+
} else if (self.lastContentOffset.x < scrollView.contentOffset.x) {
406+
scrollDirection = @"right";
407+
}
408+
} else {
409+
if (self.lastContentOffset.y > scrollView.contentOffset.y) {
410+
scrollDirection = @"up";
411+
} else if (self.lastContentOffset.y < scrollView.contentOffset.y) {
412+
scrollDirection = @"down";
413+
}
414+
}
415+
return scrollDirection;
416+
}
379417
@end

0 commit comments

Comments
 (0)