@@ -153,7 +153,7 @@ - (void)setupInitialController {
153
153
} else {
154
154
initialController = [[UIViewController alloc ] initWithView: initialView];
155
155
}
156
-
156
+
157
157
[self .cachedControllers addObject: initialController];
158
158
159
159
[self setReactViewControllers: self .initialPage
@@ -231,21 +231,40 @@ - (void)goTo:(NSInteger)index animated:(BOOL)animated {
231
231
BOOL isForward = (index > self.currentIndex && [self isLtrLayout ]) || (index < self.currentIndex && ![self isLtrLayout ]);
232
232
UIPageViewControllerNavigationDirection direction = isForward ? UIPageViewControllerNavigationDirectionForward : UIPageViewControllerNavigationDirectionReverse;
233
233
234
- NSInteger indexToDisplay = index;
234
+ self.reactPageIndicatorView .numberOfPages = numberOfPages;
235
+ self.reactPageIndicatorView .currentPage = index;
236
+ long diff = labs (index - _currentIndex);
235
237
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
+ }
238
243
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
+ }
241
251
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
243
263
with: controllerToDisplay
244
264
direction: direction
245
265
animated: animated];
246
-
247
266
}
248
-
267
+
249
268
- (UIViewController *)findAndCacheControllerForView : (UIView *)viewToDisplay {
250
269
if (!viewToDisplay) { return nil ; }
251
270
@@ -383,7 +402,7 @@ - (BOOL)isHorizontal {
383
402
384
403
- (void )scrollViewDidScroll : (UIScrollView *)scrollView {
385
404
CGPoint point = scrollView.contentOffset ;
386
-
405
+
387
406
float offset = 0 ;
388
407
389
408
if (self.isHorizontal ) {
@@ -395,12 +414,12 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
395
414
offset = (point.y - self.frame .size .height )/self.frame .size .height ;
396
415
}
397
416
}
398
-
417
+
399
418
float absoluteOffset = fabs (offset);
400
419
401
420
NSInteger position = self.currentIndex ;
402
421
403
-
422
+
404
423
BOOL isAnimatingBackwards = ([self isLtrLayout ] && offset<0 ) || (![self isLtrLayout ] && offset > 0 .05f );
405
424
if (isAnimatingBackwards){
406
425
position = self.currentIndex - 1 ;
@@ -415,7 +434,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
415
434
BOOL isLastPage = _currentIndex == lastPageIndex;
416
435
CGFloat contentOffset =[self isHorizontal ] ? scrollView.contentOffset .x : scrollView.contentOffset .y ;
417
436
CGFloat topBound = [self isHorizontal ] ? scrollView.bounds .size .width : scrollView.bounds .size .height ;
418
-
437
+
419
438
if ((isFirstPage && contentOffset <= topBound) || (isLastPage && contentOffset >= topBound)) {
420
439
CGPoint croppedOffset = [self isHorizontal ] ? CGPointMake (topBound, 0 ) : CGPointMake (0 , topBound);
421
440
scrollView.contentOffset = croppedOffset;
0 commit comments