Custom Transition Animation with User Interaction Enabled
Asked Answered
M

0

6

I have a custom full screen alert with a low alpha background. The default modal animation slides from bottom to top. I would like the alert to fade-in and fade-out. I am UIViewControllerTransitioningDelegate + UIViewControllerAnimatedTransitioning. In my animateTransition(using:) method I have something similar to this:

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let containerView = transitionContext.containerView
        if let toView = transitionContext.view(forKey: .to) {
            toView.frame = containerView.frame
            containerView.addSubview(toView)
            UIView.animate(withDuration: duration, delay: 0,
                           usingSpringWithDamping: 0.5,
                           initialSpringVelocity: 0,
                           options: UIViewAnimationOptions.allowUserInteraction,
                           animations: {toView.alpha = 1})
            transitionContext.completeTransition(true)
        }
    }

The problem is that the fade effect looks good at around 1-1.5 seconds; however, the user cannot tap any buttons such as "yes" or "no" during this transition animation. I attempted the following:

  • Move the call transitionContext.completeTransition(true) outside the completion handler for the animation so it happens as soon as the animation starts
  • Adding the UIViewAnimationOptions.allowUserInteraction option
  • Starting the alpha at 0.1 (and 1.0) as was suggested in another post
  • I did notice UIViewControllerInteractiveTransitioning but it seems this is only allowing interaction with the animation itself

The only way I can see this working is if I do the fade-in effect in viewDidAppear in the VC and the fade-out effect in UIViewControllerAnimatedTransitioning. Feels bad man. What is the correct way to do this?

Meshwork answered 19/9, 2017 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.