Skip to content

Commit a2430d6

Browse files
authored
fix(ios): onScroll callback values (#435)
1 parent bb783a3 commit a2430d6

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

ios/ReactNativePageView.m

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ - (void)setupInitialController {
153153
} else {
154154
initialController = [[UIViewController alloc] initWithView:initialView];
155155
}
156-
156+
157157
[self.cachedControllers addObject:initialController];
158158

159159
[self setReactViewControllers:self.initialPage
@@ -231,21 +231,40 @@ - (void)goTo:(NSInteger)index animated:(BOOL)animated {
231231
BOOL isForward = (index > self.currentIndex && [self isLtrLayout]) || (index < self.currentIndex && ![self isLtrLayout]);
232232
UIPageViewControllerNavigationDirection direction = isForward ? UIPageViewControllerNavigationDirectionForward : UIPageViewControllerNavigationDirectionReverse;
233233

234-
NSInteger indexToDisplay = index;
234+
self.reactPageIndicatorView.numberOfPages = numberOfPages;
235+
self.reactPageIndicatorView.currentPage = index;
236+
long diff = labs(index - _currentIndex);
235237

236-
UIView *viewToDisplay = self.reactSubviews[indexToDisplay];
237-
UIViewController *controllerToDisplay = [self findAndCacheControllerForView:viewToDisplay];
238+
if (isForward && diff > 0) {
239+
for (NSInteger i=_currentIndex+1; i<=index; i++) {
240+
[self goToViewController:i direction:direction animated:animated];
241+
}
242+
}
238243

239-
self.reactPageIndicatorView.numberOfPages = numberOfPages;
240-
self.reactPageIndicatorView.currentPage = indexToDisplay;
244+
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];
248+
}
249+
}
250+
}
241251

242-
[self setReactViewControllers:indexToDisplay
252+
if (diff == 0) {
253+
[self goToViewController:index direction:direction animated:animated];
254+
}
255+
}
256+
257+
- (void)goToViewController:(NSInteger)index
258+
direction:(UIPageViewControllerNavigationDirection)direction
259+
animated:(BOOL)animated {
260+
UIView *viewToDisplay = self.reactSubviews[index];
261+
UIViewController *controllerToDisplay = [self findAndCacheControllerForView:viewToDisplay];
262+
[self setReactViewControllers:index
243263
with:controllerToDisplay
244264
direction:direction
245265
animated:animated];
246-
247266
}
248-
267+
249268
- (UIViewController *)findAndCacheControllerForView:(UIView *)viewToDisplay {
250269
if (!viewToDisplay) { return nil; }
251270

@@ -383,7 +402,7 @@ - (BOOL)isHorizontal {
383402

384403
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
385404
CGPoint point = scrollView.contentOffset;
386-
405+
387406
float offset = 0;
388407

389408
if (self.isHorizontal) {
@@ -395,12 +414,12 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
395414
offset = (point.y - self.frame.size.height)/self.frame.size.height;
396415
}
397416
}
398-
417+
399418
float absoluteOffset = fabs(offset);
400419

401420
NSInteger position = self.currentIndex;
402421

403-
422+
404423
BOOL isAnimatingBackwards = ([self isLtrLayout] && offset<0) || (![self isLtrLayout] && offset > 0.05f);
405424
if(isAnimatingBackwards){
406425
position = self.currentIndex - 1;
@@ -415,7 +434,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
415434
BOOL isLastPage = _currentIndex == lastPageIndex;
416435
CGFloat contentOffset =[self isHorizontal] ? scrollView.contentOffset.x : scrollView.contentOffset.y;
417436
CGFloat topBound = [self isHorizontal] ? scrollView.bounds.size.width : scrollView.bounds.size.height;
418-
437+
419438
if ((isFirstPage && contentOffset <= topBound) || (isLastPage && contentOffset >= topBound)) {
420439
CGPoint croppedOffset = [self isHorizontal] ? CGPointMake(topBound, 0) : CGPointMake(0, topBound);
421440
scrollView.contentOffset = croppedOffset;

0 commit comments

Comments
 (0)