iOS 控制器自定義動(dòng)畫跳轉(zhuǎn)方法(模態(tài)跳轉(zhuǎn))
參考資料:
Apple 開發(fā)文檔 Customizing the Transition Animations
WWDC 2013 Custom Transitions Using View Controllers
圖例:
跳轉(zhuǎn)的動(dòng)畫有很多,全部可以自定義
創(chuàng)建自定義跳轉(zhuǎn)必須遵循的三個(gè)步驟:
1、創(chuàng)建一個(gè)類,并實(shí)現(xiàn)了 UIViewControllerAnimatedTransitioning 協(xié)議
2、創(chuàng)建一個(gè)類作為 UIViewControllerTransitioningDelegate 過渡代理
3、在模態(tài)跳轉(zhuǎn)前修改控制器的 transitioningDelegate 代理為自定義的代理(步驟2的代理類)
核心代碼示例
一、創(chuàng)建一個(gè)類,并實(shí)現(xiàn)了 UIViewControllerAnimatedTransitioning 協(xié)議
這個(gè)協(xié)議主要控制控制器視圖的顯示的,通過 transitionContext 可以獲取到每個(gè)視圖和控制器,并進(jìn)行動(dòng)畫的設(shè)置
class AnimatedTransitioning: NSObject {
var isPresenting: Bool = false
}
extension AnimatedTransitioning: UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.5
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let fromView = transitionContext.view(forKey: .from)!
let toView = transitionContext.view(forKey: .to)!
let containerView = transitionContext.containerView
if isPresenting {
toView.transform = CGAffineTransform(scaleX: 0, y: 0)
containerView.addSubview(toView)
} else {
containerView.insertSubview(toView, belowSubview: fromView)
}
UIView.animate(withDuration: 0.5, animations: {
if self.isPresenting {
toView.transform = CGAffineTransform.identity
} else {
fromView.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
}
}) { (finished) in
transitionContext.completeTransition(finished)
}
}
}
二、創(chuàng)建一個(gè)類作為 UIViewControllerTransitioningDelegate 過渡代理
這里設(shè)置 presented 和 dismissed 時(shí)各自的動(dòng)畫轉(zhuǎn)換類,可以設(shè)置為不同的類
class CustomTransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
let at = AnimatedTransitioning()
at.isPresenting = true
return at
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
let at = AnimatedTransitioning()
at.isPresenting = false
return at
}
}
三、在模態(tài)跳轉(zhuǎn)前修改控制器的 transitioningDelegate 代理為自定義的代理
注意:代理不能為局部變量
class ViewController: UIViewController {
// 必須保存為實(shí)例變量
var ctDelegate = CustomTransitioningDelegate()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let vc = TempViewController()
vc.transitioningDelegate = ctDelegate
self.present(vc, animated: true, completion: nil)
}
}
以上這篇iOS 控制器自定義動(dòng)畫跳轉(zhuǎn)方法(模態(tài)跳轉(zhuǎn))就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
iOS實(shí)現(xiàn)視頻下載并自動(dòng)保存到相冊(cè)功能
這篇文章主要為大家詳細(xì)介紹了ios 視頻下載功能實(shí)現(xiàn),并自動(dòng)保存到相冊(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
iOS開發(fā)系列--詳細(xì)介紹數(shù)據(jù)存取
本篇文章主要介紹了iOS開發(fā)系列--詳細(xì)介紹數(shù)據(jù)存取,詳細(xì)介紹了IOS數(shù)據(jù)的存儲(chǔ)問題,具有一定的參考價(jià)值,有興趣的同學(xué)可以了解一下。2016-11-11
iOS 使用 socket 實(shí)現(xiàn)即時(shí)通信示例(非第三方庫)
這篇文章主要介紹了iOS 使用 socket 即時(shí)通信示例(非第三方庫)的資料,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2017-02-02
理解iOS多線程應(yīng)用的開發(fā)以及線程的創(chuàng)建方法
這篇文章主要介紹了理解iOS多線程應(yīng)用的開發(fā)以及線程的創(chuàng)建方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-11-11
iOS如何自定義控制器轉(zhuǎn)場(chǎng)動(dòng)畫push詳解
在平時(shí)開發(fā)中,有時(shí)候需要一些轉(zhuǎn)場(chǎng)動(dòng)畫給界面調(diào)整增添一些活力,而實(shí)現(xiàn)這些動(dòng)畫相對(duì)比較繁瑣。下面這篇文章主要給大家介紹了關(guān)于iOS如何自定義控制器轉(zhuǎn)場(chǎng)動(dòng)畫push的相關(guān)資料,需要的朋友可以參考下。2017-12-12
iOS 對(duì)NSMutableArray進(jìn)行排序和過濾的實(shí)例
下面小編就為大家分享一篇iOS 對(duì)NSMutableArray進(jìn)行排序和過濾的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01

