ios5中就引入了這個 UIPageViewController,它也是一種controller容器,提供了2種在controller之間切換的動畫,一種是很普通的滑動效果,另一種是很炫的書翻頁效果。如果你的程序有多個在功能上並列的controller,並且適合使用以上2種動畫進行切換,那么就應當使用UIPageViewController。
關於UIPageViewController,有2個重要的屬性,這2個屬性都是在采用書翻頁效果的動畫時才有用的,一個是spineLocation,一個是doubleSided。其中spineLocation表示書脊的位置,有min,mid,max三個選項,其中,min為書脊居左或上,mid為居中,max為居右或下。doubleSided表示是否采用雙面顯示。
建立一個UIPageViewController模板的最簡單的方法是利用xcode的模板,如下圖:
UIPageViewController 的特點是,
1.它不同於navigation controller,沒有自己的stack來保存以前的controller,它的示例代碼也是每次都創建新的controller。雖然可以使用其他方法恢復以前的狀態,但是可以看出,UIPageViewController的目的是創建一些不需要保存狀態的controller,更多是為了展示圖片等內容用的,而不是另外一種navigation controller。
2.它每次顯示的controller的個數是1個或是2個,具體是1個還是2個,需要看使用的過渡動畫和doubleSided,spineLocation屬性。具體為:
2.1 當使用UIPageViewControllerTransitionStyleScroll時,一次只能顯示1個viewcontroller。
2.2 當使用UIPageViewControllerTransitionStylePageCurl時,通過調用
- (void)setViewControllers:(NSArray *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^)(BOOL finished))completion;
一次可以顯示2個controller,如下表
Spine location |
Double sided |
What to pass |
---|---|---|
|
|
Pass the page to be displayed on the left and the page to be displayed on the right. |
|
|
Pass the front of the page to be displayed and the back of the previously-displayed page. The back is used for the page turning animation. |
|
|
Pass the front of the page to be displayed. |
2.2.1 從表中可以看出,當spine的位置在中間時,doubleSieded必須設置為YES,原文是這樣寫的If the spine is located in the middle, the value must be YES
. Setting it to NO
with the spine located in the middle raises an exception.其實規定容易理解,因為如果采用了spine在中間,那么就有如下效果:
這樣的翻頁,翻頁的背面當然要有新一頁的內容,這樣才像生活中使用的書。背面的效果如下圖:
2.2.2
根據文檔所說,當doubleSided是yes時,Spine location 不是mid時,也應該傳2個controller給
- (void)setViewControllers:(NSArray *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^)(BOOL finished))completion;
但是,我傳入2個就會報錯,不明白原因。上面的demo的效果如下:
3.如果采用了書籍翻頁效果,那么最好保持所有的controller實例都產生於同一個類,僅僅更改顯示的信息,這樣就能保證界面的統一,看起來更像書籍。