swiftanimationuistoryboardsegueunwind-segue

How To Create Animation of Segue perform and unwind segue?


i am working on application where i am trying to call controllers with animations second view controller with animations like when i call second controller with segue second controller come from right and first controller start moving to left like android push pop animations

i make some code

class SegueFromRight: UIStoryboardSegue {
override func perform() {
    let src = self.source
    let dst = self.destination

    src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
    dst.view.transform = CGAffineTransform(translationX: src.view.frame.size.width*2, y: 0)
    //Double the X-Axis
    UIView.animate(withDuration: 0.25, delay: 0.0, options: UIView.AnimationOptions.curveEaseInOut, animations: {
        dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
    }) { (finished) in
        src.present(dst, animated: false, completion: nil)
    }



}

}

but from this code new controller come from right i need to move current controller to move left too if anyone can help


Solution

  • Try this code:

    class CustomSegue: UIStoryboardSegue {
        override func perform() {
            let firstVCView: UIView = self.source.view
            let secondVCView: UIView = self.destination.view
            self.destination.modalPresentationStyle = .fullScreen
    
            let screenWidth = UIScreen.main.bounds.size.width
            let screenHeight = UIScreen.main.bounds.size.height
    
            secondVCView.frame = CGRect(x: screenWidth, y: 0, 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: -screenWidth, dy: 0.0)
                secondVCView.frame = secondVCView.frame.offsetBy(dx: -screenWidth, dy: 0.0)
                   }) { (Finished) -> Void in
                    self.source.present(self.destination as UIViewController,
                           animated: false,
                           completion: nil)
               }
        }
    }
    
    
    class CustomSegueUnwind: UIStoryboardSegue {
        override func perform() {
            let secondVCView: UIView = self.source.view
            let firstVCView: UIView = self.destination.view
            self.destination.modalPresentationStyle = .fullScreen
    
            let screenWidth = UIScreen.main.bounds.size.width
    
            let window = UIApplication.shared.keyWindow
            window?.insertSubview(firstVCView, aboveSubview: secondVCView)
    
            UIView.animate(withDuration: 0.4, animations: { () -> Void in
                firstVCView.frame = firstVCView.frame.offsetBy(dx: screenWidth, dy: 0.0)
                secondVCView.frame = secondVCView.frame.offsetBy(dx: screenWidth, dy: 0.0)
                   }) { (Finished) -> Void in
                    self.source.dismiss(animated: false, completion: nil)
               }
        }
    }
    

    Reference HERE for more details