Dave Dave - 7 months ago 16
Swift Question

Is it possible to create Custom Segue which transitions from right to left

I'm creating an iOS app with Swift and I'd like to create custom segue which transitions from right to left. The default transition way is from left to right but I do need to have the opposite way of transition. I just wrote the code like this.

class RightToLeftSegue: UIStoryboardSegue {

override func perform() {
let sourceViewController = self.sourceViewController
let destinationViewController = self.destinationViewController

let window = UIApplication.sharedApplication().keyWindow
window!.rootViewController = destinationViewController
window!.rootViewController = sourceViewController
UIView.transitionWithView(window!, duration: 0.5, options: .TransitionFlipFromLeft, animations: { window!.rootViewController = destinationViewController }, completion: { (finished: Bool) in })
}
}


But this just transitions flipping move. Any way I can do this? Thanks!

Answer

Try this

class RightToLeftSegue : UIStoryboardSegue {

     override func perform() {

         if let window = UIApplication.sharedApplication().keyWindow as? UIWindow { 

             window.rootViewController = sourceViewController
             sourceViewController.view.superview?.addSubview(destinationViewController.view)

             let frame = sourceViewController.view.frame
             destinationViewController.view.frame = CGRectOffset(frame, frame.size.width, 0.0)

             UIView.animateWithDuration(0.5,
             animations: {
                 sourceViewController.view.frame = CGRectOffset(frame, -frame.size.width, 0.0)
                 destinationViewController.view.frame = frame
             })
             { (finished: Bool) in 
                 window.rootViewController = destinationViewController
             }

         }

    }

}
Comments