Method example of IOS swift to realize transition animation

Time:2022-5-27

Introduction to transition animation

Transition animation is often encountered in our daily development. The so-called transition animation is the animation effect in the process of changing the view of one controller to the view of another controller. This example is to pop up a controller slowly next to the titleview of the navigation controller. Let’s take a look at the detailed introduction:

design sketch:


Before the special session


After the special session

Sample code

First, customize an animator class. Set the agent in the controller that needs transition

//The controller titleviewvc that needs to animate the transition Transitioningdelegate = animator // Animator here is an instance of animator

Here is the code in the animator class

class animatorTool: NSObject {
 lazy var isPresent = false
 var callBack : ((isPresented:Bool)->())?// Whether the animation passed to the outside world is being displayed

 init(callBack : ((isPresented:Bool)->())) {
  self.callBack = callBack
 }//Customize the construction method to assign values to closures
}
extension animatorTool:UIViewControllerTransitioningDelegate{
 func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
  Return awypresentationcontroller (presentedviewcontroller: presented, presentingviewcontroller: presenting) // awypresentationcontroller is a custom class inherited from uipresentationcontroller to set the size of VC's view from modal
 }
 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 // animation duration
 }
 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) // leave a value for animation
  }) { (_) in
   transitonContext.completeTransition(true)
  }

 }


}

summary

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message. Thank you for your support for developeppaer.