iOS Swift控制器轉(zhuǎn)場(chǎng)動(dòng)畫(huà)示例代碼
前言
在IOS開(kāi)發(fā)中,我們model另外一個(gè)控制器的時(shí)候,一般都使用默認(rèn)的轉(zhuǎn)場(chǎng)動(dòng)畫(huà)。本文將給大家詳細(xì)介紹關(guān)于iOS Swift控制器轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
返回效果也可更改
四種轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
1. move:源圖片位置移動(dòng)到目標(biāo)圖片位置;
2. circle:根據(jù)源控件大小創(chuàng)建圓形或者橢圓形path路徑,放大展示目標(biāo);
3. tier:源左右,目標(biāo)由小到大縮放;
4. middle:源的中心點(diǎn)開(kāi)始放大,返回是縮回到中心。
代碼解析
給UIViewController添加一個(gè)屬性yy_routerAnimation: YYTransition
extension UIViewController { public var yy_routerAnimation : YYTransition { set { objc_setAssociatedObject(self, &YYTransitionKey.kRouterAnimationKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } get { guard let r = objc_getAssociatedObject(self, &YYTransitionKey.kRouterAnimationKey) as? YYTransition else { return YYTransition() } return r } } }
YYTransition類(lèi)
public class YYTransition: NSObject
遵守代理
extension YYTransition: UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate, UINavigationControllerDelegate
實(shí)現(xiàn)代理方法
return self } public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { return 轉(zhuǎn)場(chǎng)動(dòng)畫(huà)所需時(shí)間 }
這個(gè)方法內(nèi)調(diào)用相應(yīng)動(dòng)畫(huà)方法
public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { switch self.yy_ransitionAnimationType { case .circle: break case .move: break case .middle: break case .tier: break } }
相關(guān)屬性
extension YYTransition { // 是push還是pop public var yy_isBack: Bool {} // 動(dòng)畫(huà)類(lèi)型 var yy_ransitionAnimationType: YYTransitionAnimationType {} // 源view名字 var yy_fromViewPath: String? { } // 目標(biāo)view名字 var yy_toViewPath: String? { } // 句柄 var yy_transitionContext: UIViewControllerContextTransitioning {} }
實(shí)現(xiàn)基礎(chǔ)動(dòng)畫(huà)結(jié)束時(shí)的代理方法
extension YYTransition: CAAnimationDelegate { public func animationDidStop(_ anim: CAAnimation, finished flag: Bool) { yy_transitionContext.completeTransition(!yy_transitionContext.transitionWasCancelled) yy_transitionContext.viewController(forKey: .from)?.view.layer.mask = nil yy_transitionContext.viewController(forKey: .to)?.view.layer.mask = nil } }
基礎(chǔ)動(dòng)畫(huà)對(duì)路徑操作的動(dòng)畫(huà)
extension YYTransition { func maskAnimation(targetVC: UIViewController, startPath: UIBezierPath, endPath: UIBezierPath, context: UIViewControllerContextTransitioning) { }
下面四個(gè)文件內(nèi)實(shí)現(xiàn)相對(duì)應(yīng)的動(dòng)畫(huà)
YYTransition+Circle YYTransition+Move YYTransition+Tier YYTransition+Middle
動(dòng)畫(huà)實(shí)現(xiàn)的思想基本就是拿到源view和目標(biāo)view,控制位置和大小,做相應(yīng)的動(dòng)畫(huà)即可。
用到的方法
UIViewControllerContextTransitioning 調(diào)用 public func viewController(forKey key: UITransitionContextViewControllerKey) -> UIViewController? UIViewController調(diào)用 open func value(forKeyPath keyPath: String) -> Any? * When requesting a snapshot, 'afterUpdates' defines whether the snapshot is representative of what's currently on screen or if you wish to include any recent changes before taking the snapshot. open func snapshotView(afterScreenUpdates afterUpdates: Bool) -> UIView? open func convert(_ rect: CGRect, from view: UIView?) -> CGRect open func insertSubview(_ view: UIView, belowSubview siblingSubview: UIView) // This must be called whenever a transition completes (or is cancelled.) // Typically this is called by the object conforming to the // UIViewControllerAnimatedTransitioning protocol that was vended by the transitioning // delegate. For purely interactive transitions it should be called by the // interaction controller. This method effectively updates internal view // controller state at the end of the transition. public func completeTransition(_ didComplete: Bool)
具體代碼在YE項(xiàng)目地址中YYTransition動(dòng)態(tài)庫(kù)中
eg在YYSourceTransitionViewController和YYTargetTransitionViewController中可以看到。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
iOS動(dòng)畫(huà)案例(1) 類(lèi)似于qq賬號(hào)信息里的一個(gè)動(dòng)畫(huà)效果
做一個(gè)類(lèi)似于qq賬號(hào)信息里的一個(gè)動(dòng)畫(huà),感覺(jué)挺有意思,下面給大家分享iOS動(dòng)畫(huà)案例(1) 類(lèi)似于qq賬號(hào)信息里的一個(gè)動(dòng)畫(huà)效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-01-01提高iOS開(kāi)發(fā)的小技巧和思路小結(jié) (二)
這篇文章主要跟大家分享了關(guān)于提高iOS開(kāi)發(fā)的一些小技巧和思路,通過(guò)本文總結(jié)的這些小技巧和思路相信對(duì)對(duì)大家開(kāi)發(fā)iOS具有一定的參考價(jià)值,感興趣的朋友們可以參考學(xué)習(xí),下面來(lái)跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2017-04-04iOS開(kāi)發(fā)中使用UILabel設(shè)置字體的相關(guān)技巧小結(jié)
這篇文章主要介紹了iOS開(kāi)發(fā)中UILabel設(shè)置字體的相關(guān)技巧小結(jié),代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01Unity iOS混合開(kāi)發(fā)界面切換思路解析
這篇文章主要介紹了Unity iOS混合開(kāi)發(fā)界面切換思路解析的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09詳細(xì)整理iOS中UITableView的性能優(yōu)化
最近在微博上看到一個(gè)很好的開(kāi)源項(xiàng)目,是關(guān)于如何優(yōu)化UITableView的,加上正好最近也在優(yōu)化項(xiàng)目中的類(lèi)似朋友圈功能這塊,思考了很多關(guān)于UITableView的優(yōu)化技巧,所以決定詳細(xì)的整理下對(duì)優(yōu)化UITableView的理解,需要的朋友們可以參考借鑒。2017-03-03UITableView中Cell重用機(jī)制導(dǎo)致內(nèi)容重復(fù)的解決方法
這篇文章主要為大家詳細(xì)介紹了UITableView中Cell重用機(jī)制導(dǎo)致內(nèi)容重復(fù)的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06