首先陷舅,UIPageViewController坑很多,下面文章都有描述
1魏颓、翻頁(yè)方式
一共四種翻頁(yè)方式(UIPageViewControllerTransitionStyle)
-
滑動(dòng)翻頁(yè)(UIPageViewControllerTransitionStyleScroll)
點(diǎn)擊翻頁(yè)
手勢(shì)翻頁(yè)
-
仿真翻頁(yè)(UIPageViewControllerTransitionStylePageCurl)
點(diǎn)擊翻頁(yè)
手勢(shì)翻頁(yè)
手勢(shì)翻頁(yè)直接設(shè)置好UIPageViewControllerTransitionStyle后滑動(dòng)就可以實(shí)現(xiàn)。點(diǎn)擊翻頁(yè)通過(guò)UIPageViewController子頁(yè)面添加點(diǎn)擊手勢(shì)UIPanGestureRecognizer實(shí)現(xiàn)
2、手勢(shì)翻頁(yè)
手勢(shì)主要通過(guò)的UIPageViewController代理實(shí)現(xiàn)
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController;
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;
注意:這兩種方法屬于提供頁(yè)面數(shù)據(jù)源的代理遍烦,并不是頁(yè)面翻到前一頁(yè)婿崭、后一頁(yè)的回調(diào)
所以無(wú)法通過(guò)這兩個(gè)方法來(lái)設(shè)置頁(yè)面的頁(yè)碼數(shù)
UIPageViewControllerTransitionStylePageCurl模式下可以通過(guò)從pageViewController取得當(dāng)前頁(yè)面再根據(jù)是after或是before將頁(yè)碼加減拨拓。
但是UIPageViewControllerTransitionStyleScroll模式下,你手勢(shì)翻頁(yè)上面兩種方法會(huì)調(diào)用多次氓栈,它其實(shí)是一種數(shù)據(jù)源的代理渣磷,例如你從第二頁(yè)翻到第三頁(yè),它會(huì)要求你提供第四頁(yè)的數(shù)據(jù)颤绕。因?yàn)槭謩?shì)翻頁(yè)需要提前準(zhǔn)備好下一頁(yè)的數(shù)據(jù)才能實(shí)現(xiàn)平滑互動(dòng)的效果幸海。從我實(shí)驗(yàn)結(jié)果看,這種模式手動(dòng)翻頁(yè)奥务,以上方法會(huì)調(diào)用三次物独,會(huì)調(diào)用兩次after方法,一次before方法氯葬。所以你無(wú)法通過(guò)這兩種方法來(lái)獲取當(dāng)前滑動(dòng)到了第幾頁(yè)挡篓。
UIPageViewControllerTransitionStyleScroll模式下使用下面兩個(gè)方法實(shí)現(xiàn)獲取當(dāng)前頁(yè)面
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed;
通過(guò)pageViewController獲取當(dāng)前頁(yè)面的頁(yè)碼page
3、點(diǎn)擊翻頁(yè)
點(diǎn)擊翻頁(yè)需要調(diào)取setViewControllers方法
dispatch_async(dispatch_get_main_queue(), ^{
[self.pageViewController setViewControllers:viewControllers direction:direction animated:animated completion:^(BOOL finished) {
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) {
completion(finished);
}
});
}];
});
注意:一定要在主線程中執(zhí)行帚称,否則會(huì)發(fā)生crash(報(bào)錯(cuò)Unbalanced calls to begin/end appearance transitions for )
setViewControllers參數(shù)是一個(gè)數(shù)組官研,如果當(dāng)時(shí)是滑動(dòng)翻頁(yè)模式時(shí)(UIPageViewControllerTransitionStyleScroll)viewControllers只需一個(gè)@[nextVc]
而處在仿真翻頁(yè)模式時(shí)(UIPageViewControllerTransitionStylePageCurl)viewControllers需要兩個(gè)@[currentVc,nextVc]