最近在學(xué)swift脚囊,就用swift寫了個小Demo[https://github.com/jiaowoxiaoming/CustomNavigaitonAnimations ]听皿。
在項目中常常看見別的App不是用的原生的Push/Pop動畫士复,都是怎么做的呢。iOS7之后翩活,蘋果就為我們做好接口了阱洪,讓我們能靈活的控制UIViewController的切換。
言歸正傳菠镇,從零開始做一個轉(zhuǎn)場動畫冗荸。
從創(chuàng)建自定義Push/Pop說起
首先創(chuàng)建工程,storyBoard中拖一個ViewController辟犀,給它加上導(dǎo)航控制器俏竞。完畢之后,需要考慮動畫的實現(xiàn)方式堂竟,恩魂毁,用transform動畫吧。很簡單的放大縮小動畫出嘹。
因為我們需要自定義動畫席楚,所以需要實現(xiàn)UINavigationControllerDelegate
協(xié)議方法。具體方法如下:
func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
//這里用到了一個屬性來記錄當(dāng)前操作的類型(Push or Pop)
navigationOperation = operation
return self
}
在這里再贅述一下UINavigationControllerDelegate
其他協(xié)議方法(已經(jīng)了解的當(dāng)我沒說)
//這個方法就是切換橫豎屏?xí)r調(diào)用的操作 設(shè)置方法設(shè)置導(dǎo)航控制器支持的設(shè)備方向
@available(iOS 7.0, *)
optional public func navigationControllerSupportedInterfaceOrientations(navigationController: UINavigationController) -> UIInterfaceOrientationMask
//這個方法設(shè)置導(dǎo)航控制器的首選設(shè)備方向
@available(iOS 7.0, *)
optional public func navigationControllerPreferredInterfaceOrientationForPresentation(navigationController: UINavigationController) -> UIInterfaceOrientation
//下面兩個方法可以對導(dǎo)航的轉(zhuǎn)場動畫進行設(shè)置 1.交互式動畫 2.傳統(tǒng)的push/pop 動畫
@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?
然后控制器需要實現(xiàn)另外一個協(xié)議UIViewControllerAnimatedTransitioning
它有三個方法:
// 動畫時間
public func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval
// 具體動畫操作
public func animateTransition(transitionContext: UIViewControllerContextTransitioning)
//動畫結(jié)束的回調(diào)方法
optional public func animationEnded(transitionCompleted: Bool)
需要實現(xiàn)animateTransition
和transitionDuration
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView()
let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
let fromeViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
var destView:UIView!
var destTransform:CGAffineTransform!
if navigationOperation == UINavigationControllerOperation.Push {
containerView?.insertSubview((toViewController?.view)!, aboveSubview: (fromeViewController?.view)!)
destView = toViewController?.view
destView.transform = CGAffineTransformMakeScale(0.1, 0.1)
destTransform = CGAffineTransformMakeScale(1, 1)
}
else if navigationOperation == UINavigationControllerOperation.Pop
{
containerView?.insertSubview((toViewController?.view)!, belowSubview: (fromeViewController?.view)!)
destView = fromeViewController?.view
destTransform = CGAffineTransformMakeScale(0.1, 0.1)
}
UIView .animateWithDuration(transitionDuration(transitionContext), animations: {
destView.transform = destTransform!
}, completion: ({completed in
transitionContext.completeTransition(true)
}))
}
到此一個自定義的導(dǎo)航轉(zhuǎn)場動畫就完成了税稼,至于具體的動畫烦秩,在此我只是拋磚引玉,大家完全可以寫出更絢麗的動畫郎仆。