- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- iOS如何自定義轉場(chǎng)動(dòng)畫(huà)
這篇文章給大家分享的是有關(guān)iOS如何自定義轉場(chǎng)動(dòng)畫(huà)的內容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
預備
首先,我們現在介紹幾個(gè)在自定義轉場(chǎng)動(dòng)畫(huà)時(shí)需要接觸的協(xié)議:
UIViewControllerAnimatedTransitioning: 實(shí)現此協(xié)議的實(shí)例控制轉場(chǎng)動(dòng)畫(huà)效果。UIViewControllerInteractiveTransitioning: 實(shí)現此協(xié)議的實(shí)例控制著(zhù)利用手勢過(guò)渡時(shí)的進(jìn)度處理。
我們在定義好了實(shí)現上面兩個(gè)協(xié)議的類(lèi)后,只需要在需要進(jìn)行轉場(chǎng)的地方,提供對應的對象即可。
ps:下面的實(shí)例中,請大家忽略動(dòng)畫(huà)效果,關(guān)注實(shí)現。(其實(shí)是懶得去寫(xiě)太多動(dòng)畫(huà)了。??♂?)
模態(tài)跳轉(Present)
場(chǎng)景
self.present(vc!, animated: true) {}self.dismiss(animated: true) {}
實(shí)現步驟
設置將要 present 的 ViewController 的 transitioningDelegate 對象,此對象是實(shí)現協(xié)議 UIViewControllerTransitioningDelegate 的實(shí)例。實(shí)現 UIViewControllerTransitioningDelegate 協(xié)議中的幾個(gè)代理方法,返回實(shí)現了 UIViewControllerAnimatedTransitioning 協(xié)議的動(dòng)畫(huà)效果控制類(lèi)。
需要實(shí)現的UIViewControllerTransitioningDelegate方法:
//返回用于 present 的自定義 transition 動(dòng)畫(huà)optional func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?//返回用于 dismiss 的自定義 transition 動(dòng)畫(huà)optional func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
實(shí)例
/// 第一個(gè) VC 中點(diǎn)擊跳轉func presentClick(_ sender: Any) { let vc = self.storyboard?.instantiateViewController(withIdentifier: "PresentSecondViewController") vc?.modalPresentationStyle = .fullScreen vc?.transitioningDelegate = self self.present(vc!, animated: true) {}}// 第一個(gè) VC 實(shí)現協(xié)議,返回控制轉場(chǎng)動(dòng)畫(huà)效果的實(shí)例extension PresentFirstViewController: UIViewControllerTransitioningDelegate { func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { return NormalPresentAnimator() } func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { return NormalPresentAnimator() }}
導航控制器跳轉(Push)
場(chǎng)景
self.navigationController?.pushViewController(vc!, animated: true)self.navigationController?.popViewController(animated: true)
實(shí)現步驟
設置導航控制器 UINavigationController 的 delegate。實(shí)現 UINavigationControllerDelegate 協(xié)議中的代理方法,返回實(shí)現了 UIViewControllerAnimatedTransitioning 協(xié)議的動(dòng)畫(huà)效果控制類(lèi)。
需要實(shí)現的UINavigationControllerDelegate方法:
optional func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?
實(shí)例
class PushFirstViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.navigationController?.delegate = self } @IBAction func pushClick(_ sender: Any) { let vc = self.storyboard?.instantiateViewController(withIdentifier: "PushSecondViewController") self.navigationController?.pushViewController(vc!, animated: true) }}extension PushFirstViewController: UINavigationControllerDelegate { //返回自定義過(guò)渡動(dòng)畫(huà) func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? { if operation == .pop && fromVC is PushFirstViewController { return nil } return NormalPushAnimator() }}
UITabbarController
在前面的兩個(gè)專(zhuān)場(chǎng)實(shí)現中,我們在需要轉場(chǎng)的類(lèi)中分別實(shí)現了UIViewControllerTransitioningDelegate 及 UINavigationControllerDelegate 方法,在這兩個(gè)協(xié)議中,還有這樣幾個(gè)方法:
/// UIViewControllerTransitioningDelegateoptional func interactionControllerForPresentation(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?optional func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?/// UINavigationControllerDelegateoptional func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?
上面這幾個(gè)方法呢?其實(shí)就是我們通過(guò)利用手勢轉場(chǎng)時(shí)過(guò)渡的進(jìn)度處理方法。我們需要在代理方法中返回一個(gè)實(shí)現了 UIViewControllerInteractiveTransitioning 協(xié)議的對象來(lái)對轉場(chǎng)進(jìn)度進(jìn)行控制。下面的 UITabbarController 中我就實(shí)現一個(gè)利用手勢控制轉場(chǎng)的例子。 Present 及 Push/Pop 按照相同的思路實(shí)現即可。
場(chǎng)景
UITabbarController 在默認的狀態(tài)下,切換控制器時(shí)是沒(méi)有動(dòng)畫(huà)效果的。如果需要動(dòng)畫(huà)效果的話(huà),需要我們進(jìn)行自定義。
實(shí)現步驟
設置 UITabbarController 的 delegate。實(shí)現 UITabBarControllerDelegate 協(xié)議中的代理方法,返回實(shí)現了 UIViewControllerAnimatedTransitioning 協(xié)議的動(dòng)畫(huà)效果控制類(lèi),以及返回實(shí)現了 UIViewControllerInteractiveTransitioning 協(xié)議的轉場(chǎng)進(jìn)度控制類(lèi)。
/// 返回實(shí)現了 UIViewControllerAnimatedTransitioning 協(xié)議的實(shí)例func tabBarController(_ tabBarController: UITabBarController, animationControllerForTransitionFrom fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?/// 返回實(shí)現了 UIViewControllerInteractiveTransitioning 協(xié)議的實(shí)例func tabBarController(_ tabBarController: UITabBarController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?
實(shí)例
class TabbarController: UITabBarController, UITabBarControllerDelegate { override func viewDidLoad() { super.viewDidLoad() self.delegate = self}func tabBarController(_ tabBarController: UITabBarController, animationControllerForTransitionFrom fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? { if self.selectedIndex == 0 { return TabbarAnimator(edge: .right) } else { return TabbarAnimator(edge: .left) }}func tabBarController(_ tabBarController: UITabBarController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? { if self.panGesture.state == .began || self.panGesture.state == .changed { return TabbarInteractionTransition(pan: self.panGesture) } else { return nil }}
三方框架——Lottie
介紹
Lottie 是 Android 和 iOS 的移動(dòng)庫,用 bodymovin 解析 Adobe After Effects 導出為 json 的動(dòng)畫(huà)并在移動(dòng)設備上生成矢量動(dòng)畫(huà)。設計師可以輕松的創(chuàng )建漂亮(復雜)的動(dòng)畫(huà),無(wú)需程序員辛苦地手動(dòng)去創(chuàng )建及調試。
場(chǎng)景
實(shí)現一些特殊的轉場(chǎng),且程序員無(wú)足夠時(shí)間調試動(dòng)畫(huà)時(shí)。
實(shí)現步驟
在工程中導入 Lottie 框架。在需要轉場(chǎng)的類(lèi)中,將 Lottie import。因為 Lottie 實(shí)現的轉場(chǎng)實(shí)際上是 Present 的轉場(chǎng),所以設置將要 Present 的控制器的 transitioningDelegate。實(shí)現 UIViewControllerTransitioningDelegate 協(xié)議中的幾個(gè)代理方法,返回利用轉場(chǎng)動(dòng)畫(huà) json 文件初始化的 LOTAnimationTransitionController 的實(shí)例。
ps:Lottie 轉場(chǎng)的 LOTAnimationTransitionController 在 3.0.0 版本后被移除,所以需要使用 Lottie 做轉場(chǎng)時(shí),需要在導入時(shí),指定版本號為更早的版本。我這里使用的是 2.5.3。
實(shí)例
/// 第一個(gè) VCfunc presentClick(_ sender: Any) { let vc = self.storyboard?.instantiateViewController(withIdentifier: "LottieSecondViewController") vc?.transitioningDelegate = self self.present(vc!, animated: true) {}}/// 實(shí)現 UIViewControllerTransitioningDelegate,返回 LOTAnimationTransitionController 的實(shí)例extension LottieFirstViewController: UIViewControllerTransitioningDelegate { func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { let transitionController = LOTAnimationTransitionController(animationNamed: "Count", fromLayerNamed: "", toLayerNamed: "", applyAnimationTransform: false) return transitionController } func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { let transitionController = LOTAnimationTransitionController(animationNamed: "Three", fromLayerNamed: "", toLayerNamed: "", applyAnimationTransform: false) return transitionController }}
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站