@@ -18,12 +18,14 @@ @interface ReactNativePageView () <UIPageViewControllerDataSource, UIPageViewCon
18
18
@property (nonatomic , weak ) UIView *currentView;
19
19
20
20
@property (nonatomic , strong ) NSHashTable <UIViewController *> *cachedControllers;
21
+ @property (nonatomic , assign ) CGPoint lastContentOffset;
21
22
22
23
- (void )goTo : (NSInteger )index animated : (BOOL )animated ;
23
24
- (void )shouldScroll : (BOOL )scrollEnabled ;
24
25
- (void )shouldShowPageIndicator : (BOOL )showPageIndicator ;
25
26
- (void )shouldDismissKeyboard : (NSString *)dismissKeyboard ;
26
27
28
+
27
29
@end
28
30
29
31
@implementation ReactNativePageView {
@@ -349,8 +351,13 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
349
351
[self .eventDispatcher sendEvent: [[RCTOnPageScrollStateChanged alloc ] initWithReactTag: self .reactTag state: @" idle" coalescingKey: _coalescingKey++]];
350
352
}
351
353
354
+ - (BOOL )isHorizontal {
355
+ return self.orientation == UIPageViewControllerNavigationOrientationHorizontal;
356
+ }
357
+
352
358
- (void )scrollViewDidScroll : (UIScrollView *)scrollView {
353
359
CGPoint point = scrollView.contentOffset ;
360
+
354
361
float offset = 0 ;
355
362
356
363
if (!_overdrag) {
@@ -361,7 +368,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
361
368
}
362
369
}
363
370
364
- if (self.orientation == UIPageViewControllerNavigationOrientationHorizontal ) {
371
+ if (self.isHorizontal ) {
365
372
if (self.frame .size .width != 0 ) {
366
373
offset = (point.x - self.frame .size .width )/self.frame .size .width ;
367
374
}
@@ -370,10 +377,41 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
370
377
offset = (point.y - self.frame .size .height )/self.frame .size .height ;
371
378
}
372
379
}
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;
375
393
}
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)]];
377
398
}
378
399
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
+ }
379
417
@end
0 commit comments