新聞中心
轉(zhuǎn)場動畫介紹
成都創(chuàng)新互聯(lián)公司是一家專注于網(wǎng)站建設(shè)、成都做網(wǎng)站與策劃設(shè)計(jì),威遠(yuǎn)網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:威遠(yuǎn)等地區(qū)。威遠(yuǎn)做網(wǎng)站價(jià)格咨詢:18980820575
轉(zhuǎn)場動畫在我們?nèi)粘i_發(fā)中是經(jīng)常遇到的,所謂轉(zhuǎn)場動畫,就是一個(gè)控制器的view切到另一個(gè)控制器的view上過程中過的動畫效果。本例子是實(shí)現(xiàn)了在導(dǎo)航控制器的titleView邊上慢慢彈出一個(gè)控制器。下面話不多說,來一起看看詳細(xì)的介紹:
效果圖:
專場前
專場后
示例代碼
首先自定義一個(gè)animator類。在需要轉(zhuǎn)場的控制器內(nèi),設(shè)置代理
//需要設(shè)置轉(zhuǎn)場動畫的控制器titleViewVc.transitioningDelegate = aniamator//這里的animator是animator的實(shí)例
下面是animator類中的代碼
class animatorTool: NSObject { lazy var isPresent = false var callBack : ((isPresented:Bool)->())?//向外界傳遞動畫是否正在顯示 init(callBack : ((isPresented:Bool)->())) { self.callBack = callBack }//自定義構(gòu)造方法,便于給閉包賦值 } extension animatorTool:UIViewControllerTransitioningDelegate{ func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? { return AWYPresentationController(presentedViewController: presented, presentingViewController: presenting)//AWYPresentationController是自定義繼承自UIPresentationController的類,是為了設(shè)置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//動畫時(shí)長 } 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)//留一點(diǎn)值,這樣會有動畫效果 }) { (_) in transitonContext.completeTransition(true) } } }
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對創(chuàng)新互聯(lián)的支持。
新聞名稱:iOSswift實(shí)現(xiàn)轉(zhuǎn)場動畫的方法示例
標(biāo)題URL:http://ef60e0e.cn/article/jhohje.html