1.需求確認(rèn)
? ? ? 在HomeViewController中, 點(diǎn)擊TitleButton, 通過(guò)自定義轉(zhuǎn)場(chǎng)動(dòng)畫的方式modal出一小塊的PopoverViewController
2.類索引
? ? ? 1) HomeViewController : BaseViewController
-- "源控制器", 負(fù)責(zé)發(fā)起自定義轉(zhuǎn)場(chǎng)
? ? ? 2) PopoverViewController : UIViewController
-- "目標(biāo)控制器", 通過(guò)自定義轉(zhuǎn)場(chǎng)而被展現(xiàn)
? ? ? 3) PopoverPresentationController : UIPresentationController
-- "轉(zhuǎn)場(chǎng)控制器", 負(fù)責(zé)配置自身容器屬性, 將目標(biāo)控制器的view添加到容器中, 容器本質(zhì)也是一個(gè)view
? ? ? 4) PopoverAnimator : NSObject
-- 自定義轉(zhuǎn)場(chǎng)動(dòng)畫類, 用于將下面兩個(gè)代理中的方法從源控制器中進(jìn)行抽離, 使源控制器只保留邏輯代碼, 保證其代碼的整潔和優(yōu)雅
? ? ? 5) UIViewControllerTransitioningDelegate
?-- 常用三個(gè)代理方法, 用于告訴系統(tǒng)誰(shuí)是轉(zhuǎn)場(chǎng)控制器, 誰(shuí)負(fù)責(zé)轉(zhuǎn)場(chǎng)動(dòng)畫的開始, 誰(shuí)負(fù)責(zé)轉(zhuǎn)場(chǎng)動(dòng)畫的結(jié)束
? ? ? 6) UIViewControllerAnimatedTransitioning
-- 常用兩個(gè)代理方法, 用于告訴系統(tǒng)轉(zhuǎn)場(chǎng)動(dòng)畫的持續(xù)時(shí)間與實(shí)現(xiàn)
3.實(shí)現(xiàn)步驟
1) 設(shè)置目標(biāo)控制器的代理對(duì)象
popoverViewController.transitioningDelegate = 自定義轉(zhuǎn)場(chǎng)動(dòng)畫類的實(shí)例化對(duì)象
2) 設(shè)置目標(biāo)控制器的轉(zhuǎn)場(chǎng)樣式
popoverViewController.modalPresentationStyle= UIModalPresentationStyle.Custom
3) 自定義轉(zhuǎn)場(chǎng)控制器類 PopoverPresentationController : UIPresentationController
在這個(gè)類中, 著重使用了兩個(gè)屬性, 并重寫兩個(gè)方法:
containerView : 容器屬性, 所有被展現(xiàn)的東西都在這個(gè)view內(nèi)部
presentedView() : 返回目標(biāo)控制器的view
init(presentedViewController, presentingViewController)
初始化方法, 記得調(diào)用'super.'對(duì)父類初始化
第一個(gè)參數(shù): 目標(biāo)控制器
第二個(gè)參數(shù): 在Xcode6為nil, Xcode7為野指針
containerViewWillLayoutSubviews()
容器布局方法, 該方法會(huì)在轉(zhuǎn)場(chǎng)前進(jìn)行調(diào)用, 對(duì)容器內(nèi)的視圖進(jìn)行統(tǒng)一配置(包括: 目標(biāo)控制器的view.frame蟀拷、coverView等)
4) 實(shí)現(xiàn)UIViewControllerTransitioningDelegate的最主要代理方法
presentationControllerForPresentedViewController(presented,presenting, source)
-> UIPresentationController?
該代理方法告訴系統(tǒng), 誰(shuí)是轉(zhuǎn)場(chǎng)控制器
參數(shù)一: 目標(biāo)控制器
參數(shù)二: 在Xcode6為nil, Xcode7為野指針
參數(shù)三: 源控制器
返回值: 轉(zhuǎn)場(chǎng)控制器
5) 實(shí)現(xiàn)UIViewControllerTransitioningDelegate的另外兩個(gè)代理方法
animationControllerForPresentedController(presented, presenting, source)
-> UIViewControllerAnimatedTransitioning?
該代理方法告訴系統(tǒng), 誰(shuí)負(fù)責(zé)轉(zhuǎn)場(chǎng)動(dòng)畫的展現(xiàn), 在展現(xiàn)開始前被調(diào)用
返回值: 簽訂UIViewControllerAnimatedTransitioning協(xié)議的對(duì)象, 這里返回轉(zhuǎn)場(chǎng)動(dòng)畫類
animationControllerForDismissedController(dismissed)
-> UIViewControllerAnimatedTransitioning?
該代理方法告訴系統(tǒng), 誰(shuí)負(fù)責(zé)轉(zhuǎn)場(chǎng)動(dòng)畫的消失, 在消失開始前被調(diào)用
返回值: 簽訂UIViewControllerAnimatedTransitioning協(xié)議的對(duì)象, 這里返回轉(zhuǎn)場(chǎng)動(dòng)畫類
6) 實(shí)現(xiàn)UIViewControllerAnimatedTransitioning的兩個(gè)代理方法
transitionDuration(transitionContext) ->NSTimeInterval
該代理方法告訴系統(tǒng), 轉(zhuǎn)場(chǎng)動(dòng)畫的執(zhí)行時(shí)間
animateTransition(transitionContext)
在該代理方法中完成動(dòng)畫的實(shí)現(xiàn), 無(wú)論展現(xiàn)還是消失,都會(huì)調(diào)用這個(gè)方法
參數(shù): transitionContext, 里面包含了動(dòng)畫需要的所有參數(shù)!
通過(guò) transitionContext.viewControllerForKey(key) 獲取目標(biāo)控制器和源控制器
通過(guò) transitionContext.viewForKey(key) 獲取展現(xiàn)的view和消失的view
通過(guò) transitionContext.containerView 獲取轉(zhuǎn)場(chǎng)控制器的容器屬性
通過(guò) transitionDuration(transitionContext) 設(shè)置動(dòng)畫持續(xù)時(shí)間
通過(guò) transitionContext.completeTransition(true), 動(dòng)畫完成時(shí)告訴系統(tǒng), 該步必須有!
7) 一切準(zhǔn)備就緒, 調(diào)用modal方法吧
presentViewController(homeVC, animated:true, completion:nil)
4.注意事項(xiàng)
1) 自定義轉(zhuǎn)場(chǎng)需要iOS8以上版本?
2) 默認(rèn)轉(zhuǎn)場(chǎng)會(huì)將源控制器的view替換成目標(biāo)控制器的view, 而自定義轉(zhuǎn)場(chǎng),不會(huì)移除源控制器的view
3) 重寫步驟5和6里面的代理方法后, 系統(tǒng)自帶的轉(zhuǎn)場(chǎng)動(dòng)畫(從下至上)就已經(jīng)沒有了
4) 在代理方法animateTransition(transitionContext)中, 通過(guò)使用transitionContext.viewForKey(key)獲取到要展現(xiàn)的視圖后,一定要將其添加到容器中,?否則彈窗彈不出來(lái)!
5) 在轉(zhuǎn)場(chǎng)動(dòng)畫完成后, 一定要調(diào)用transitionContext.completeTransition(true), 否則會(huì)產(chǎn)生各種未知的問(wèn)題(如同在layoutSubviews()中不調(diào)用super一樣)
6) 在UIView.animateWithDuration()中, 如果調(diào)用了CGAffineTransformMakeScale等參數(shù)類型為CGFloat的方法, 填0.0會(huì)沒有動(dòng)畫, 由于系統(tǒng)缺陷導(dǎo)致CGFloat類型是不準(zhǔn)確的.