iOS如何自定義轉場動畫-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關iOS如何自定義轉場動畫的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

成都創(chuàng)新互聯(lián)技術團隊10年來致力于為客戶提供網站建設、成都網站設計成都品牌網站建設、網絡營銷推廣、搜索引擎SEO優(yōu)化等服務。經過多年發(fā)展,公司擁有經驗豐富的技術團隊,先后服務、推廣了成百上千網站,包括各類中小企業(yè)、企事單位、高校等機構單位。

預備

首先,我們現(xiàn)在介紹幾個在自定義轉場動畫時需要接觸的協(xié)議:

UIViewControllerAnimatedTransitioning: 實現(xiàn)此協(xié)議的實例控制轉場動畫效果。UIViewControllerInteractiveTransitioning: 實現(xiàn)此協(xié)議的實例控制著利用手勢過渡時的進度處理。

我們在定義好了實現(xiàn)上面兩個協(xié)議的類后,只需要在需要進行轉場的地方,提供對應的對象即可。

ps:下面的實例中,請大家忽略動畫效果,關注實現(xiàn)。(其實是懶得去寫太多動畫了。??♂?)

模態(tài)跳轉(Present)

場景

self.present(vc!, animated: true) {}self.dismiss(animated: true) {}

實現(xiàn)步驟

  1. 設置將要 present 的 ViewController 的 transitioningDelegate 對象,此對象是實現(xiàn)協(xié)議 UIViewControllerTransitioningDelegate 的實例。實現(xiàn) UIViewControllerTransitioningDelegate 協(xié)議中的幾個代理方法,返回實現(xiàn)了 UIViewControllerAnimatedTransitioning 協(xié)議的動畫效果控制類。

需要實現(xiàn)的UIViewControllerTransitioningDelegate方法:

//返回用于 present 的自定義 transition 動畫optional func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?//返回用于 dismiss 的自定義 transition 動畫optional func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?

實例

/// 第一個 VC 中點擊跳轉func presentClick(_ sender: Any) {  let vc = self.storyboard?.instantiateViewController(withIdentifier: "PresentSecondViewController")  vc?.modalPresentationStyle = .fullScreen  vc?.transitioningDelegate = self  self.present(vc!, animated: true) {}}// 第一個 VC 實現(xiàn)協(xié)議,返回控制轉場動畫效果的實例extension PresentFirstViewController: UIViewControllerTransitioningDelegate { func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {  return NormalPresentAnimator() } func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {  return NormalPresentAnimator() }}

導航控制器跳轉(Push)

場景

self.navigationController?.pushViewController(vc!, animated: true)self.navigationController?.popViewController(animated: true)

實現(xiàn)步驟

  1. 設置導航控制器 UINavigationController 的 delegate。實現(xiàn) UINavigationControllerDelegate 協(xié)議中的代理方法,返回實現(xiàn)了 UIViewControllerAnimatedTransitioning 協(xié)議的動畫效果控制類。

需要實現(xiàn)的UINavigationControllerDelegate方法:

optional func navigationController(_ navigationController: UINavigationController,        animationControllerFor operation: UINavigationController.Operation,        from fromVC: UIViewController,        to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?

實例

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 { //返回自定義過渡動畫 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

在前面的兩個專場實現(xiàn)中,我們在需要轉場的類中分別實現(xiàn)了UIViewControllerTransitioningDelegate 及 UINavigationControllerDelegate 方法,在這兩個協(xié)議中,還有這樣幾個方法:

/// UIViewControllerTransitioningDelegateoptional func interactionControllerForPresentation(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?optional func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?/// UINavigationControllerDelegateoptional func navigationController(_ navigationController: UINavigationController,          interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?

上面這幾個方法呢?其實就是我們通過利用手勢轉場時過渡的進度處理方法。我們需要在代理方法中返回一個實現(xiàn)了 UIViewControllerInteractiveTransitioning 協(xié)議的對象來對轉場進度進行控制。下面的 UITabbarController 中我就實現(xiàn)一個利用手勢控制轉場的例子。 Present 及 Push/Pop 按照相同的思路實現(xiàn)即可。

場景

UITabbarController 在默認的狀態(tài)下,切換控制器時是沒有動畫效果的。如果需要動畫效果的話,需要我們進行自定義。

實現(xiàn)步驟

  1. 設置 UITabbarController 的 delegate。實現(xiàn) UITabBarControllerDelegate 協(xié)議中的代理方法,返回實現(xiàn)了 UIViewControllerAnimatedTransitioning 協(xié)議的動畫效果控制類,以及返回實現(xiàn)了 UIViewControllerInteractiveTransitioning 協(xié)議的轉場進度控制類。

/// 返回實現(xiàn)了 UIViewControllerAnimatedTransitioning 協(xié)議的實例func tabBarController(_ tabBarController: UITabBarController,       animationControllerForTransitionFrom fromVC: UIViewController,       to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?/// 返回實現(xiàn)了 UIViewControllerInteractiveTransitioning 協(xié)議的實例func tabBarController(_ tabBarController: UITabBarController,       interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?

實例

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 的移動庫,用 bodymovin 解析 Adobe After Effects 導出為 json 的動畫并在移動設備上生成矢量動畫。設計師可以輕松的創(chuàng)建漂亮(復雜)的動畫,無需程序員辛苦地手動去創(chuàng)建及調試。

場景

實現(xiàn)一些特殊的轉場,且程序員無足夠時間調試動畫時。

實現(xiàn)步驟

  1. 在工程中導入 Lottie 框架。在需要轉場的類中,將 Lottie import。因為 Lottie 實現(xiàn)的轉場實際上是 Present 的轉場,所以設置將要 Present 的控制器的 transitioningDelegate。實現(xiàn) UIViewControllerTransitioningDelegate 協(xié)議中的幾個代理方法,返回利用轉場動畫 json 文件初始化的 LOTAnimationTransitionController 的實例。

ps:Lottie 轉場的 LOTAnimationTransitionController 在 3.0.0 版本后被移除,所以需要使用 Lottie 做轉場時,需要在導入時,指定版本號為更早的版本。我這里使用的是 2.5.3。

實例

/// 第一個 VCfunc presentClick(_ sender: Any) { let vc = self.storyboard?.instantiateViewController(withIdentifier: "LottieSecondViewController") vc?.transitioningDelegate = self self.present(vc!, animated: true) {}}/// 實現(xiàn) UIViewControllerTransitioningDelegate,返回 LOTAnimationTransitionController 的實例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 }}

感謝各位的閱讀!關于“iOS如何自定義轉場動畫”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

標題名稱:iOS如何自定義轉場動畫-創(chuàng)新互聯(lián)
轉載來源:http://bm7419.com/article36/gddpg.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供Google、做網站、云服務器、品牌網站建設、域名注冊、服務器托管

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網站建設