iOS swift實(shí)現(xiàn)轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的方法示例
轉(zhuǎn)場(chǎng)動(dòng)畫(huà)介紹
轉(zhuǎn)場(chǎng)動(dòng)畫(huà)在我們?nèi)粘i_(kāi)發(fā)中是經(jīng)常遇到的,所謂轉(zhuǎn)場(chǎng)動(dòng)畫(huà),就是一個(gè)控制器的view切到另一個(gè)控制器的view上過(guò)程中過(guò)的動(dòng)畫(huà)效果。本例子是實(shí)現(xiàn)了在導(dǎo)航控制器的titleView邊上慢慢彈出一個(gè)控制器。下面話(huà)不多說(shuō),來(lái)一起看看詳細(xì)的介紹:
效果圖:
專(zhuān)場(chǎng)前
專(zhuān)場(chǎng)后
示例代碼
首先自定義一個(gè)animator類(lèi)。在需要轉(zhuǎn)場(chǎng)的控制器內(nèi),設(shè)置代理
//需要設(shè)置轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的控制器titleViewVc.transitioningDelegate = aniamator//這里的animator是animator的實(shí)例
下面是animator類(lèi)中的代碼
class animatorTool: NSObject { lazy var isPresent = false var callBack : ((isPresented:Bool)->())?//向外界傳遞動(dòng)畫(huà)是否正在顯示 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的類(lèi),是為了設(shè)置modal出來(lái)的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//動(dòng)畫(huà)時(shí)長(zhǎng) } 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)值,這樣會(huì)有動(dòng)畫(huà)效果 }) { (_) in transitonContext.completeTransition(true) } } }
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- IOS實(shí)戰(zhàn)之自定義轉(zhuǎn)場(chǎng)動(dòng)畫(huà)詳解
- 詳解iOS開(kāi)發(fā)中的轉(zhuǎn)場(chǎng)動(dòng)畫(huà)和組動(dòng)畫(huà)以及UIView封裝動(dòng)畫(huà)
- IOS輕松幾步實(shí)現(xiàn)自定義轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
- 實(shí)例講解iOS中的CATransition轉(zhuǎn)場(chǎng)動(dòng)畫(huà)使用
- 深入學(xué)習(xí)iOS7自定義導(dǎo)航轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
- iOS實(shí)現(xiàn)類(lèi)似格瓦拉電影的轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
- IOS登錄頁(yè)面動(dòng)畫(huà)、轉(zhuǎn)場(chǎng)動(dòng)畫(huà)開(kāi)發(fā)詳解
- iOS如何自定義控制器轉(zhuǎn)場(chǎng)動(dòng)畫(huà)push詳解
- 詳解IOS圖層轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
- iOS實(shí)現(xiàn)轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的3種方法示例
相關(guān)文章
詳解IOS判斷當(dāng)前網(wǎng)絡(luò)狀態(tài)的三種方法
這篇文章主要介紹了詳解IOS判斷當(dāng)前網(wǎng)絡(luò)狀態(tài)的三種方法,網(wǎng)絡(luò)狀態(tài)是非常重要的知識(shí),感興趣的同學(xué),必須要看一下2021-04-04IOS 開(kāi)發(fā)之Object-C中的對(duì)象詳解
這篇文章主要介紹了IOS 開(kāi)發(fā)之Object-C中的對(duì)象詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06IOS 簽名錯(cuò)誤codesign failed with exit code 1解決方法
這篇文章主要介紹了IOS 簽名錯(cuò)誤codesign failed with exit code 1解決方法的相關(guān)資料,遇到同樣問(wèn)題的朋友可以看下,這里提供了解決方案,需要的朋友可以參考下2017-01-01IOS10 隱私權(quán)限設(shè)置實(shí)例詳解
這篇文章主要介紹了IOS10 隱私權(quán)限設(shè)置實(shí)例詳解的相關(guān)資料,本文主要是介紹,在使用相機(jī)功能是遇到錯(cuò)誤,這里說(shuō)明該如何解決,需要的朋友可以參考下2016-12-12詳解iOS App開(kāi)發(fā)中改變UIButton內(nèi)部控件的基本方法
這篇文章主要介紹了iOS App開(kāi)發(fā)中改變UIButton內(nèi)部控件的基本方法,文章開(kāi)頭也順帶總結(jié)了一些UIButton的基本用法,示例代碼為Objective-C,需要的朋友可以參考下2016-03-03