ios8中 利用UIPresentationController 自訂轉場
1、UIPresentationController 介紹
UIPresentationController 是 iOS8 新增的一個 API,用來控制 controller 之間的跳轉特效。比如希望實現一個特效,顯示一個視窗,大小和位置都是自訂的,並且遮罩在原來的頁面上。在之前,可以操作view的一些方法來實現。
2、使用介紹
1. 設定目標控制器的 轉場代理 和 展示方法
controller.modalPresentationStyle = UIModalPresentationStyle.Custom// 設定 動畫樣式controller.transitioningDelegate = transtinoDelegate// 此對象要實現 UIViewControllerTransitioningDelegate 協議
2. transtionDelegate 實現 UIViewControllerTransitioningDelegate 協議方法
// 返回控制控制器彈齣動畫的對象optional func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?// 返回控制控制器消失動畫的對象optional func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?@availability(iOS, introduced=8.0)// 返回控制控制器跳轉的對象optional func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController!, sourceViewController source: UIViewController) -> UIPresentationController?
3. 控制控制器跳轉的類
控制控制器跳轉的類繼承自 UIPresentationController
/*構造方法:參數:presentedViewController將要跳轉到的目標控制器presentingViewController跳轉前的原控制器*/init(presentedViewController: UIViewController!, presentingViewController: UIViewController!)
常用的屬性和方法
presentedViewController:要 modal 顯示的視圖控制器presentingViewController:低層的視圖控制器containerView()[內容] 檢視presentedView()被展現的視圖func presentationTransitionWillBegin()跳轉將要開始func presentationTransitionDidEnd(completed: Bool)跳轉完成func dismissalTransitionWillBegin()dismiss 將要開始func dismissalTransitionDidEnd(completed: Bool)dismiss 完成func frameOfPresentedViewInContainerView()目標 控制器設定
4. 控制動畫類
控制動畫對類繼承自 UIViewControllerAnimatedTransitioning
// 執行動畫的時間func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval// 動畫過程設定func animateTransition(transitionContext: UIViewControllerContextTransitioning)
5. 總結
1. 設定目標控制器的轉場代理
2. 專場代理返回三個對象用於控制轉場過程
3. 控制控制器轉場對象,可以在此做專場前後的操作,例如添加遮罩等
4. 控制轉場動畫,用於設定目標控制器出現或消失的動畫,以及控制器的大小樣式等。
3. API 詳細介紹
稍後。。。
4.自訂跳轉 Demo
稍後。。。