酷炫的轉(zhuǎn)場(chǎng)動(dòng)畫難的是想象,不是實(shí)現(xiàn)。
UIViewControllerTransitioning.h里面包含了所有你想要的
其中 ** UIViewControllerContextTransitioning ** 協(xié)議是轉(zhuǎn)場(chǎng)動(dòng)畫上下文,看其屬性就能看到,它提供了大多部分轉(zhuǎn)場(chǎng)相關(guān)的細(xì)節(jié)內(nèi)容
你可以拿到容器視圖 :** containerView() -> UIView? **
還可以拿到動(dòng)畫的主要兩個(gè)頁面:** viewForKey(key: String) -> UIView? **
// viewForKey: may return nil which would indicate that the animator should not
// manipulate the associated view controller's view.
@available(iOS 8.0, *)
public func viewForKey(key: String) -> UIView?
key:UITransitionContextFromViewKey 或者 UITransitionContextToViewKey
在 Modal 轉(zhuǎn)場(chǎng)里要注意,從上面可以知道,Custom 模式下,fromView 并不受 containerView 管理,這時(shí)通過viewForKey:方法來獲取 fromView 得到的是 nil.但是你可以用下面的方法來搞
獲取視圖 ** public func viewControllerForKey(key: String) -> UIViewController? **
key:UITransitionContextToViewControllerKey, and UITransitionContextFromViewControllerKey. 再通過.view 來拿到對(duì)應(yīng)的 to/from View
好了 還有常用切很重要的 ** public func completeTransition(didComplete: Bool)
**
動(dòng)畫執(zhí)行后 必須寫上這句話。 還有一個(gè) ** transitionWasCancelled() -> Bool **
下面這些看注釋就清楚了,跟交互相關(guān)的,在實(shí)現(xiàn)交互的時(shí)候也就這幾個(gè)函數(shù)。下面細(xì)講
public func isInteractive() -> Bool // This indicates whether the transition is currently interactive.//是否是與用戶交互控制的動(dòng)畫
// It only makes sense to call these from an interaction controller that
// conforms to the UIViewControllerInteractiveTransitioning protocol and was
// vended to the system by a container view controller's delegate or, in the case
// of a present or dismiss, the transitioningDelegate.
public func updateInteractiveTransition(percentComplete: CGFloat)
public func finishInteractiveTransition()
public func cancelInteractiveTransition()
實(shí)現(xiàn)一個(gè)簡(jiǎn)單的轉(zhuǎn)場(chǎng)動(dòng)畫
1.實(shí)現(xiàn)對(duì)應(yīng)的控制器代理方法,簡(jiǎn)單告訴他返回你已經(jīng)實(shí)現(xiàn)了轉(zhuǎn)場(chǎng)動(dòng)畫協(xié)議的類對(duì)象,或者實(shí)現(xiàn)了交互協(xié)議的類對(duì)象就好了。 注意如果當(dāng)前不是交互控制轉(zhuǎn)場(chǎng)的,在轉(zhuǎn)場(chǎng)協(xié)議里面要返回nil。
push,pop轉(zhuǎn)場(chǎng) navi-UINavigationControllerDelegate
@available(iOS 7.0, *)
optional public func navigationController(navigationController: UINavigationController, interactionControllerForAnimationController animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?
@available(iOS 7.0, *)
optional public func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?
selelcted-UITabBarControllerDelegate
@available(iOS 7.0, *)
optional public func tabBarController(tabBarController: UITabBarController, interactionControllerForAnimationController animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?
@available(iOS 7.0, *)
optional public func tabBarController(tabBarController: UITabBarController, animationControllerForTransitionFromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?
present,dismiss - transitioningDelegate
@available(iOS 2.0, *)
optional public func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?
@available(iOS 2.0, *)
optional public func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
optional public func interactionControllerForPresentation(animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?
optional public func interactionControllerForDismissal(animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?
@available(iOS 8.0, *)
optional public func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController?
含有interactionControllerForAnimationController是交互協(xié)議,animationControllerForDismissedController是告訴代理,轉(zhuǎn)場(chǎng)的時(shí)候要返回我自己的轉(zhuǎn)場(chǎng)動(dòng)畫控制器。比如這里我返回了一個(gè)已經(jīng)實(shí)現(xiàn)UIViewControllerAnimatedTransitioning的動(dòng)畫控制器
extension PushOneVC:UINavigationControllerDelegate {
func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
if operation == .Pop && fromVC == self {
return nil
}
let type:CircleTransitionType = operation == .Push ? CircleTransitionType.NavTransitionPush :CircleTransitionType.NavTransitionPop
return CirclePushAnimationController(type: type)
}
2.實(shí)現(xiàn)轉(zhuǎn)場(chǎng)動(dòng)畫協(xié)議里面的兩個(gè)方法
class CirclePushAnimationController: NSObject,UIViewControllerAnimatedTransitioning {
//告訴他動(dòng)畫的時(shí)常
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 0.5
}
//這個(gè)是最主要的,告訴他執(zhí)行什么動(dòng)畫
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
...
1.拿到容器視圖containerView,fromVC,toVC
2.將要執(zhí)行動(dòng)畫的視圖 toView 加到containerView中,其中fromView 系統(tǒng)已經(jīng)自動(dòng)加入.
** 注意,如果當(dāng)前是present切style是Custom,那么在dismiss的時(shí)候就不要加toView了,你可以這樣簡(jiǎn)單理解,一般在轉(zhuǎn)場(chǎng)結(jié)束completeTransition后,會(huì)自動(dòng)將fromView從容器中移除。但是Custom類型的時(shí)候卻沒有移除,你可以明顯的看到。因此在dismiss的時(shí)候,之前的fromView 也就變成了toView。**
3.書寫酷炫的動(dòng)畫代碼,一般使用UIView 的類方法 animaitonWithDur....,如果對(duì)toView/fromView的layer做動(dòng)畫就用CABaseAnimation去做。
4.在completion回調(diào)或者animationdidStop中記得寫上 completeTransition(ture)很重要
}
}
3.實(shí)現(xiàn)手勢(shì)交互
在每一個(gè)需要交互控制轉(zhuǎn)場(chǎng)動(dòng)畫的根圖層上加一個(gè)pan手勢(shì),根據(jù)手勢(shì)的狀態(tài)或者拖動(dòng)比例以及系統(tǒng)提供的updateInteractiveTransition(0.7)等方法來控制轉(zhuǎn)場(chǎng)進(jìn)度。
注意一點(diǎn)要在對(duì)應(yīng)的交互代理方法interactionControllerForPresentation中判斷當(dāng)前是不是正在進(jìn)行手勢(shì)交互,如果是的話才返回對(duì)應(yīng)的交互控制器,如果不是的話就返回nil
系統(tǒng)提供了一個(gè)手勢(shì)交互控制器,UIPercentDrivenInteractiveTransition。當(dāng)然你也可以繼承他,寫一個(gè)自己的手勢(shì)交互控制器。記得一點(diǎn),每一個(gè)需要交互控制轉(zhuǎn)場(chǎng)動(dòng)畫的視圖都需要一個(gè)自己的交互手勢(shì)。如fromVC 和 toVC都應(yīng)該有一個(gè)自己的交互手勢(shì)。這也不難理解。
下面是手勢(shì)交互控制轉(zhuǎn)場(chǎng)進(jìn)度的代碼
switch gesture.state {
case .Began :
interation = true
self.startTransition()
case .Changed :
updateInteractiveTransition(percent)
case .Ended :
completionSpeed = 0.99
interation = false
if percent > 0.7 {
finishInteractiveTransition()
}else {
cancelInteractiveTransition()
}
case .Cancelled :
interation = false
completionSpeed = 0.99
cancelInteractiveTransition()
default :
break
}
}
func startTransition() {
switch type {
case .Presente, .Push :
if let closure = config {
closure()//執(zhí)行nav.pushViewController()或者self.presentViewController
}
case .Pop:
vc?.navigationController?.popViewControllerAnimated(true)
case .Dismiss:
vc?.dismissViewControllerAnimated(true, completion: nil)
}
}
最后說一下,你可以通過使用截圖snapshotViewAfterScreenUpdates:NO(YES代表立即刷新視圖然后截圖),對(duì)截圖和toView進(jìn)行動(dòng)畫操作,來做一些cell的轉(zhuǎn)場(chǎng)動(dòng)畫。 你也可以通過layer.mask屬性做最上面gif圖的轉(zhuǎn)場(chǎng)效果,對(duì)這個(gè)屬性有疑問的,可以百度搜a(bǔ)lpha通道,會(huì)幫助理解。