title: How to solve this problem with custom transition in iOS?
body: <p>I built a class to implement a circular transition between view controllers. When I hit the button to navigate to the other view controller a circle starts growing from the button until it fills the screen with the new controller. When I dismiss the view controller I expected this circle to shrink down back to the original position. It's also working. The only problem is that when the dismiss is underway the back of the screen while the circle is shrinking is completely black and after the animation is completed the new viewController appears abruptly.</p>
  
  <p>Here are some photos of the effect:</p>
  
  <p><a href="https://i.stack.imgur.com/PAL01.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PAL01.png" alt="enter image description here"></a></p>
  
  <p>Here's the code of the custom class:</p>
  
  <pre><code>class customTransition: NSObject, UIViewControllerAnimatedTransitioning{
  
  var duration: TimeInterval = 0.5
  var startPoint = CGPoint.zero
  
  var circle = UIView()
  
  var circleColor = UIColor.white
  enum transitMode: Int {
      case presenting, dismissing
  }
  
  var transitionMode: transitMode = .presenting
  
  func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -&gt; TimeInterval {
      return duration
  }
  
  func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
  
      let container = transitionContext.containerView
      guard let to = transitionContext.view(forKey: UITransitionContextViewKey.to) else {return}
      guard let from = transitionContext.view(forKey: UITransitionContextViewKey.from) else {return}
  
      circleColor = to.backgroundColor ?? UIColor.white
  
      if transitionMode == .presenting {
          to.translatesAutoresizingMaskIntoConstraints = false
          to.center = startPoint
  
          circle = UIView()
          circle.backgroundColor = circleColor
          circle.frame = getFrameForCircle(rect: to.frame)
          circle.layer.cornerRadius = circle.frame.width / 2
          circle.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
          circle.alpha = 0
  
  
          circle.addSubview(to)
  
          to.centerXAnchor.constraint(equalTo: circle.centerXAnchor).isActive = true
          to.centerYAnchor.constraint(equalTo: circle.centerYAnchor).isActive = true
          to.widthAnchor.constraint(equalToConstant: to.frame.width).isActive = true
          to.heightAnchor.constraint(equalToConstant: to.frame.height).isActive = true
  
          container.addSubview(circle)
  
          UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: UIView.AnimationOptions.curveLinear, animations: {
              self.circle.center = from.center
              self.circle.transform = CGAffineTransform.identity
              self.circle.alpha = 1
  
          }) { (sucess) in
  
              transitionContext.completeTransition(sucess)
  
          }
  
      } else if transitionMode == .dismissing {
  
  
          UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: UIView.AnimationOptions.curveLinear, animations: {
              self.circle.center = self.startPoint
              self.circle.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
              self.circle.alpha = 0
  
          }) { (sucess) in
              transitionContext.completeTransition(sucess)
          }
      }
  }
  
  
  
  
  func getFrameForCircle(rect: CGRect) -&gt; CGRect{
      let width = Float(rect.width)
      let height = Float(rect.height)
  
      let diameter = CGFloat(sqrtf(width * width + height * height))
  
      let x: CGFloat = rect.midX - (diameter / 2)
      let y: CGFloat = rect.midY - (diameter / 2)
  
     return CGRect(x: x, y: y, width: diameter, height: diameter)
  }
  
  
  
  }
  </code></pre>
  
  <p>and the implementation...</p>
  
  <pre><code>    let circularTransition = customTransition()
  </code></pre>
  
  <p>the call for the present view controller... I tried to set <code>secondVC.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext</code> but when I set this line it ignores completely the animation transition I don't know why...
  `</p>
  
  <pre><code> @objc func handlePresent(sender: UIButton){
      let secondVC = nextVC()
      secondVC.transitioningDelegate = self
      present(secondVC, animated: true, completion: nil)
  
  }
  </code></pre>
  
  <p>delegate methods:</p>
  
  <pre><code>  func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -&gt; UIViewControllerAnimatedTransitioning? {
      circularTransition.startPoint = presentButton.center
      circularTransition.transitionMode = .presenting
      return circularTransition
  }
  
  func animationController(forDismissed dismissed: UIViewController) -&gt; UIViewControllerAnimatedTransitioning? {
      circularTransition.transitionMode = .dismissing
      circularTransition.startPoint = presentButton.center
  
      return circularTransition
  }
  </code></pre>
  
  <p>What am I missing here? Any suggestions?
  No storyboard being used, just code.</p>