欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-18 21:12:34
转场动画介绍 转场动画在我们日常开发中是经常遇到的,所谓转场动画,就是一个控制器的view切到另一个控制器的view上过程中过的动画效果。本例子是实现了在导航控制器的ti...

转场动画介绍

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

效果图:

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

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

示例代码

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

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

下面是animator类中的代码

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)
  }

 }


}

总结

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

上一篇:

下一篇: