关于Page翻页效果--Page View Controller
Page View Controllers
你使用一個page view controller用page by page的方式來展示內容。一個page view controller管理一個self-contained視圖架構。這個架構的父視圖由page View controller管理,并且子視圖由你提供的view Controllers管理。
一,解析Page View Controller
一個page view controller有一個單獨的視圖,是你的內容的存放的地方。
下圖顯示了page view interface的結構:最外面的棕色視圖是父視圖控制器,不是page view controller自身。page view controller沒有自己的UI;然而,它對它的children做一個翻頁效果。
?
二,page view controller interface的對象:
一個可選的代理,一個可選的數據源,一組current view controllers,一組手勢識別者
?
數據源按需提供viewControllers。
委托提供響應基于手勢的導航和方向更改。
view Controllers的數組包含當前要顯示的content view controller。數組中的items的數量基于傳遞給page view controller的選項。
手勢識別者只有在數據源被指定時才存在。這些手勢識別者使用戶通過輕擊、flicking(快速滑動)或拖動翻頁。
三,創建page view controller interface:
page view controller的view可以被縮放和嵌入到一個視圖層次架構中。這意味著,不像一個導航控制器或tab bar controller,page view controller可以在廣闊的范圍內使用,而不是一些特別的情況。
1,通過stroyboard創建page view controller: Page-Based Application Xcode模版創建一個新的帶page view controller的工程。
要添加一個page view controller到一個已經存在的storyboard:
1)拖動一個page view controller,并添加一個page view controller scene 到你的storyboard.
2)在屬性檢查器中,設置適當的選項。
3)可選地,設置一個委托,數據源
4)。。。
2,通過代碼創建一個page view controller:
1)通過initWithTransitionStyle:navigationOrientation:options:方法創建一個page view controller
2)可選地,設置一個代理、數據源
3)設置初始的content view controllers
4) 在屏幕上顯示page view controller的view。
3,設置最初的view controller:無論你是通過代碼創建還是通過IB創建了一個page view controller,你都需要在將其顯示到屏幕之前指定一個初始的view controller。
調用setViewControllers:direction:animated:completion:方法。
如果page view controller沒有initial view controller,它的shouldAutorotateToInterfaceOrientation:返回NO,并拋出一個異常。
四,自定義初始化行為:
在初始化時,你傳遞給initWithTransitionStyle:navigationOrientation:options:方法的參數值和選項來自定義一個page view controller。在初始化后,這些屬性都是只讀的。你可以自定義:
1)The direction in which navigation occurs.
2)The location of the spine.(書脊的位置)
3)The transition style. In iOS 5, the only valid transition style is UIPageViewControllerTransitionStylePageCurl.
例如,
Listing 3-1? Customizing a page view controller
NSDictionary * options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:UIPageViewControllerSpineLocationMid] forKey:UIPageViewControllerOptionSpineLocationKey];
UIPageViewController *pageViewController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
五、在運行時使用代理自定義行為:
UIPageViewControllerDelegate協議:可以在設備方向更改時和用戶導航到一個新頁時執行動作,并且它可以更新書脊的位置來響應方向的轉變。
六、通過數據源提供內容:
數據源的方法被當前顯示的view controller調用,并返回它之前和之后的view controller。為了簡化查找前面和后面的viewcontroller,你可以在你的view controller里存儲額外的信息,例如一個page number。
如果指定了數據源,page view controller將關聯手勢識別到它的view。這些手勢通過gestureRecognizers屬性訪問。
要移動手勢識別到另一個view,傳遞gestureRecognizers屬性的值給目標view的addGestureRecognizer:方法。
例如,如果你的page view controller沒有充滿屏幕,你將手勢識別傳遞給其superview,可以更好地翻頁,而不用必須在page view controller內翻頁。
七,通過設置current view controller提供內容:
setViewControllers:direction:animated:completion:方法直接控制顯示哪些內容。
如果你沒有設置數據源,你需要提供UI來移動pages,例如前進和后退按鈕。手勢驅動的導航只有在在你提供了數據源的時候才可用。
八,特別考慮Right-to-Left和Bottom-to-Top Content:
要通過數據源,使用一個page view controller來顯示一個right-to-left或bottom-to-top內容,只需要反轉下面兩個方法的實現:
1)在你的數據源中,實現pageViewController:viewControllerBeforeViewController: 并返回之后的的viewController
2)pageViewController:viewControllerAfterViewController:返回前面的viewController>
并且,你一般要設置書脊的位置為UIPageViewControllerSpineLocationMax
轉載于:https://www.cnblogs.com/zhwl/archive/2013/01/05/2846331.html
總結
以上是生活随笔為你收集整理的关于Page翻页效果--Page View Controller的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 何以笙箫默下一句是什么啊?
- 下一篇: 削骨瘦脸多少钱