Tom Odell
Tom Odell

Reputation: 105

How to dismiss presentation controller with a custom animation

I'm using Custom Segue class to navigate between controllers.

class CustomSegue: UIStoryboardSegue {
    override func perform() {
        let firstVCView = self.source.view as UIView!
        let secondVCView = self.destination.view as UIView!

        let screenWidth = UIScreen.main.bounds.size.width
        let screenHeight = UIScreen.main.bounds.size.height

        secondVCView?.frame = CGRect(x: 0.0, y: screenHeight, width: screenWidth, height: screenHeight)

        let window = UIApplication.shared.keyWindow
        window?.insertSubview(secondVCView!, aboveSubview: firstVCView!)

        UIView.animate(withDuration: 0.4, animations: { () -> Void in
            firstVCView?.frame = (firstVCView?.frame.offsetBy(dx: 0.0, dy: -screenHeight))!
            secondVCView?.frame = (secondVCView?.frame.offsetBy(dx: 0.0, dy: -screenHeight))!

        }) { (Finished) -> Void in
            self.source.present(self.destination as UIViewController,
                                                            animated: false,
                                                completion: nil)
        }
    } 
}

my question is how to dismiss with the opposite animation, in dismiss method, I don't have the source/destination view to take the width and height.Thanks!

Upvotes: 1

Views: 1459

Answers (1)

Tom
Tom

Reputation: 966

You probably found out already, but for other people landing on this topic, the solution is given in this post, namely doing the animation in the ViewController and then dismiss without animation.

Swift 4 code:

@IBAction func BackAction(_ sender: Any) {  
    let transition: CATransition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
    transition.type = CATransitionType.reveal
    transition.subtype = CATransitionSubtype.fromLeft
    self.view.window!.layer.add(transition, forKey: nil)

    self.dismiss(animated: false, completion: nil)
}

Upvotes: 1

Related Questions