一誓琼、轉(zhuǎn)場(chǎng)動(dòng)畫(huà)類(lèi)型
iOS控制器轉(zhuǎn)場(chǎng)動(dòng)畫(huà)類(lèi)型可以分為非交互式轉(zhuǎn)場(chǎng)動(dòng)畫(huà)和交互式轉(zhuǎn)場(chǎng)動(dòng)畫(huà)检激。
二、轉(zhuǎn)場(chǎng)動(dòng)畫(huà)分析
2.1腹侣、轉(zhuǎn)場(chǎng)代理
自定義控制器轉(zhuǎn)場(chǎng)動(dòng)畫(huà)需要重新實(shí)現(xiàn)控制器的轉(zhuǎn)場(chǎng)代理方法UIViewControllerTransitioningDelegate叔收。
//控制器present時(shí)執(zhí)行的代理方法
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?
//控制器dismiss時(shí)執(zhí)行的代理方法
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
//交互式轉(zhuǎn)場(chǎng)時(shí)控制器present執(zhí)行的代理方法
func interactionControllerForPresentation(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?
//交互式轉(zhuǎn)場(chǎng)是控制器dismiss執(zhí)行的代理方法
func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?
//定義控制器轉(zhuǎn)場(chǎng)過(guò)程
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?
自定義轉(zhuǎn)場(chǎng)代理方法需要將PresentedViewController的modalPresentationStyle設(shè)為custom或者fullScreen下才執(zhí)行。
custom和fullScreen的區(qū)別是:
fullScreen模式:控制器presentation 后傲隶,presentingView 會(huì)被主動(dòng)移出視圖結(jié)構(gòu)饺律,在 dismissal 中 presentingView 是 toView 的角色,其將會(huì)重新加入 containerView 中跺株。
custom模式:在 presentation 后复濒,presentingView 未被移出視圖結(jié)構(gòu),在 dismissal 中乒省,不要將 presentingView 加入 containerView 中巧颈,否則本來(lái)可見(jiàn)的 presentingView 將會(huì)被移除出自身所處的視圖結(jié)構(gòu)消失不見(jiàn)。
2.2袖扛、動(dòng)畫(huà)協(xié)議
控制器的動(dòng)畫(huà)協(xié)議UIViewControllerAnimatedTransitioning主要負(fù)責(zé)動(dòng)畫(huà)的執(zhí)行過(guò)程砸泛。
//動(dòng)畫(huà)的執(zhí)行時(shí)間
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval
//動(dòng)畫(huà)的具體執(zhí)行過(guò)程在這個(gè)方法中實(shí)現(xiàn)
func animateTransition(using transitionContext: UIViewControllerContextTransitioning)
//是否使能動(dòng)畫(huà)
func animationEnded(_ transitionCompleted: Bool)
2.3、動(dòng)畫(huà)的承載
UIViewControllerContextTransitioning協(xié)議承載了動(dòng)畫(huà)的重要參數(shù)蛆封,在轉(zhuǎn)場(chǎng)之前可以得到遵守寫(xiě)的的對(duì)象transitionContext唇礁,通過(guò)這個(gè)對(duì)象可以獲取動(dòng)畫(huà)所需的參數(shù)。
//容器View娶吞,承載轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
public var containerView: UIView { get }
//獲取參與轉(zhuǎn)場(chǎng)的控制器
public func viewController(forKey key: UITransitionContextViewControllerKey) -> UIViewController?
//獲取轉(zhuǎn)場(chǎng)視圖
public func view(forKey key: UITransitionContextViewKey) -> UIView?
三垒迂、轉(zhuǎn)場(chǎng)動(dòng)畫(huà)實(shí)現(xiàn)
3.1、非交互式轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
3.1.1妒蛇、轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的具體實(shí)現(xiàn)
創(chuàng)建一個(gè)遵守UIViewControllerAnimatedTransitioning協(xié)議的類(lèi)MaskAnimatedTransition机断,用于實(shí)現(xiàn)具體的動(dòng)畫(huà)。
實(shí)現(xiàn)協(xié)議方法:
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.3
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
if modalType == .present {
presentAnimateTransition(using: transitionContext)
} else {
dismissAnimateTransition(using: transitionContext)
}
}
創(chuàng)建兩個(gè)私有的方法绣夺,分別實(shí)現(xiàn)控制器present和dismiss轉(zhuǎn)場(chǎng)時(shí)的動(dòng)畫(huà):
private func presentAnimateTransition(using transitionContext: UIViewControllerContextTransitioning) {
......
}
private func dismissAnimateTransition(using transitionContext: UIViewControllerContextTransitioning) {
......
}
創(chuàng)建一個(gè)枚舉類(lèi)型吏奸,用于判斷控制器的轉(zhuǎn)場(chǎng)類(lèi)型
enum MaskAnimatedTransitionModalType {
case present
case dismiss
}
3.1.2、重寫(xiě)轉(zhuǎn)場(chǎng)代理協(xié)議
創(chuàng)建一個(gè)新的類(lèi)陶耍,遵守控制器轉(zhuǎn)場(chǎng)代理協(xié)議UIViewControllerTransitioningDelegate奋蔚,實(shí)現(xiàn)協(xié)議方法。
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return MaskAnimatedTransition(.present)
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return MaskAnimatedTransition(.dismiss)
}
3.1.3、設(shè)置轉(zhuǎn)場(chǎng)代理
創(chuàng)建一個(gè)全局的轉(zhuǎn)場(chǎng)代理:
let maskTransitionDelegate = MaskTransitionDelegate()
將控制器的轉(zhuǎn)場(chǎng)代理transitioningDelegate設(shè)為maskTransitionDelegate泊碑,將控制器的modalPresentationStyle設(shè)為custom坤按,即可進(jìn)行控制器的模態(tài)轉(zhuǎn)場(chǎng)。
上述控制的轉(zhuǎn)場(chǎng)動(dòng)畫(huà)是一個(gè)漸入漸出的動(dòng)畫(huà)類(lèi)型馒过,動(dòng)畫(huà)時(shí)間為0.3秒臭脓。
3.2交互式轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的實(shí)現(xiàn)
交互式轉(zhuǎn)場(chǎng)動(dòng)畫(huà)相對(duì)非交互動(dòng)畫(huà)而言要多實(shí)現(xiàn)一個(gè)控制器交互過(guò)渡協(xié)議UIViewControllerInteractiveTransitioning。
3.2.1腹忽、交互式轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的實(shí)現(xiàn)
實(shí)現(xiàn)開(kāi)始交互協(xié)議方法:
func startInteractiveTransition(_ transitionContext: UIViewControllerContextTransitioning) {
if modalType == .present {
presentStartInteractiveTransition(transitionContext)
} else {
dismissStartInteractiveTransition(transitionContext)
}
}
在presentedViewController上添加一個(gè)手勢(shì)UIPanGestureRecognizer来累,用于交互:
var presentedViewController: UIViewController? = nil {
didSet {
let panGR = UIPanGestureRecognizer(target: self, action: #selector(panGestureReconizerAction(_:)))
self.presentedViewController?.view.addGestureRecognizer(panGR)
}
}
在交互方法中實(shí)現(xiàn)具體的動(dòng)畫(huà):
@objc private func panGestureReconizerAction(_ panGR: UIPanGestureRecognizer) {
......
}
3.2.2、實(shí)現(xiàn)交互轉(zhuǎn)場(chǎng)代理方法
在重寫(xiě)轉(zhuǎn)場(chǎng)代理時(shí)窘奏,實(shí)現(xiàn)兩個(gè)交互轉(zhuǎn)場(chǎng)代理方法:
public func interactionControllerForPresentation(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return nil
}
public func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
interaction.modalType = .dismiss
return interaction.isInteraction ? interaction : nil
}
3.2.3嘹锁、設(shè)置轉(zhuǎn)場(chǎng)代理
創(chuàng)建一個(gè)全局的轉(zhuǎn)場(chǎng)代理:
let maskTransitionDelegate = MaskTransitionDelegate()
將控制器的轉(zhuǎn)場(chǎng)代理transitioningDelegate設(shè)為maskTransitionDelegate,將控制器的modalPresentationStyle設(shè)為custom着裹,在控制器dismiss時(shí)领猾,即可進(jìn)行交互轉(zhuǎn)場(chǎng)。