UIPageViewController跳躍切換的問題


使用的是XHScrollMenu和UIPageViewController來構建5個頁面:

ViewController1, ViewController2, ViewController3, ViewController4, ViewController5。

XHScrollMenu和UIPageViewController左右滑動均可以控制頁面的切換。

一般情況下是正確的。

但如果點擊了menu,切換ViewController1,然后再點擊menu直接切換至ViewController5。

從ViewController5向右滑動往回切換的時候發現始終會直接切換至ViewController1,而不是ViewController4。

我用一個int變量來標識當前的頁面,以此作為跳轉的依據,但不起作用,原因是UIPageViewController調用Delegate的時候自動使用了ViewController1。

 

這可能是UIPageViewController的Bug,或者是一種緩存機制。

它的特點如下:

1.

self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

 

2.使用menu來控制切換的代碼如下

- (void)scrollMenuDidSelected:(XHScrollMenu *)scrollMenu menuIndex:(NSUInteger)selectIndex {

  [_pageViewController setViewControllers:[NSArray arrayWithObject:[self viewControllerAtIndex:selectIndex]]        direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL]

}

 

最后修改:

- (void)scrollMenuDidSelected:(XHScrollMenu *)scrollMenu menuIndex:(NSUInteger)selectIndex {

  if (selectIndex > _pageIndex) {  //前翻或者后翻的條件判斷

            __block XXViewController *blocksafeSelf = self;

            [self.pageViewController setViewControllers:[NSArray arrayWithObject:[self viewControllerAtIndex:selectIndex]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {  //這里的YES是提供一個動畫效果

                if (finished) {

                    dispatch_async(dispatch_get_main_queue(), ^{

                        [blocksafeSelf.pageViewController setViewControllers:[NSArray arrayWithObject:[blocksafeSelf viewControllerAtIndex:selectIndex]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];  //這是實際起到跳轉作用的代碼,animated:NO

                    });

                }

            }];

        } else {

            __block XXViewController *blocksafeSelf = self;

            [self.pageViewController setViewControllers:[NSArray arrayWithObject:[self viewControllerAtIndex:selectIndex]] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:^(BOOL finished){

                if (finished) {

                    dispatch_async(dispatch_get_main_queue(), ^{

                        [blocksafeSelf.pageViewController setViewControllers:[NSArray arrayWithObject:[blocksafeSelf viewControllerAtIndex:selectIndex]] direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:NULL];   

                 });

                }

            }];

        }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM