標籤:style http io ar os 使用 sp for strong
以前經常把這兩個東西當成一回事, PageViewController像電子書那樣,一頁之中可以放幾個childViewcontroller, 然後左右翻,當前frame顯示幾個viewcontroller。 UIPageControl配合scrollView是用於左右滑動翻頁的,當前frame只顯示一個viewcontroller。
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
PageViewController和UIPageControl