轉場動畫介紹
轉場動畫在我們日常開發中是經常遇到的,所謂轉場動畫,就是一個控制器的view切到另一個控制器的view上過程中過的動畫效果。本例子是實現了在導航控制器的titleView邊上慢慢彈出一個控制器。下面話不多說,來一起看看詳細的介紹:
效果圖:

專場前

專場后
示例代碼
首先自定義一個animator類。在需要轉場的控制器內,設置代理
//需要設置轉場動畫的控制器titleViewVc.transitioningDelegate = aniamator//這里的animator是animator的實例
下面是animator類中的代碼
class animatorTool: NSObject {
lazy var isPresent = false
var callBack : ((isPresented:Bool)->())?//向外界傳遞動畫是否正在顯示
init(callBack : ((isPresented:Bool)->())) {
self.callBack = callBack
}//自定義構造方法,便于給閉包賦值
}
extension animatorTool:UIViewControllerTransitioningDelegate{
func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
return AWYPresentationController(presentedViewController: presented, presentingViewController: presenting)//AWYPresentationController是自定義繼承自UIPresentationController的類,是為了設置modal出來的vc的view的大小
}
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
isPresent = true
self.callBack!(isPresented: isPresent)
return self
}
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
isPresent = false
self.callBack!(isPresented: isPresent)
return self
}
}
extension animatorTool:UIViewControllerAnimatedTransitioning{
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 0.5//動畫時長
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
isPresent ?animatetransitionForPresented(transitionContext) : animatetransitionForDismissed(transitionContext)
}
func animatetransitionForPresented(transitonContext:UIViewControllerContextTransitioning){
let aimView = transitonContext.viewForKey(UITransitionContextToViewKey)!
transitonContext.containerView()?.addSubview(aimView)
aimView.transform = CGAffineTransformMakeScale(1.0, 0.0)
UIView.animateWithDuration(transitionDuration(transitonContext), animations: {
aimView.layer.anchorPoint = CGPointMake(0.5, 0.0)
aimView.transform = CGAffineTransformIdentity
}) { (_) in
transitonContext.completeTransition(true)
}
}
func animatetransitionForDismissed(transitonContext:UIViewControllerContextTransitioning){
let aimView = transitonContext.viewForKey(UITransitionContextFromViewKey)!
transitonContext.containerView()?.addSubview(aimView)
UIView.animateWithDuration(transitionDuration(transitonContext), animations: {
aimView.layer.anchorPoint = CGPointMake(0.5, 0.0)
aimView.transform = CGAffineTransformMakeScale(1.0, 0.001)//留一點值,這樣會有動畫效果
}) { (_) in
transitonContext.completeTransition(true)
}
}
}
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。