iOS swift实现转场动画的方法示例

时间:2021-05-02

转场动画介绍

转场动画在我们日常开发中是经常遇到的,所谓转场动画,就是一个控制器的view切到另一个控制器的view上过程中过的动画效果。本例子是实现了在导航控制器的titleview边上慢慢弹出一个控制器。下面话不多说,来一起看看详细的介绍:

效果图:


专场前


专场后

示例代码

首先自定义一个animator类。在需要转场的控制器内,设置代理

? 1 //需要设置转场动画的控制器titleviewvc.transitioningdelegate = aniamator//这里的animator是animator的实例

下面是animator类中的代码

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 class animatortool: nsobject { lazy var ispresent = false var callback : ((ispresented:bool)->())?//向外界传递动画是否正在显示 init(callback : ((ispresented:bool)->())) { self.callback = callback }//自定义构造方法,便于给闭包赋值 } extension animatortool:uiviewcontrollertransitioningdelegate{ func presentationcontrollerforpresentedviewcontroller(presented: uiviewcontroller, presentingviewcontroller presenting: uiviewcontroller, sourceviewcontroller source: uiviewcontroller) -> uipresentationcontroller? { return awypresentationcontroller(presentedviewcontroller: presented, presentingviewcontroller: presenting)//awypresentationcontroller是自定义继承自uipresentationcontroller的类,是为了设置modal出来的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//动画时长 } 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)//留一点值,这样会有动画效果 }) { (_) in transitoncontext.completetransition(true) } } }

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:http://www.jianshu.com/p/afb04b49be41

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章