0

I have this code that segues left to right but I want it to do right to left. How would I go about doing this?

let screenWidth = (UIScreen.mainScreen().bounds).width
    let screenHeight = (UIScreen.mainScreen().bounds).height

    let firstVCView = sourceViewController.view
    let secondVCView = destinationViewController.view
    secondVCView.frame = CGRectMake(screenWidth, 0.0, screenWidth, screenHeight)

    // Access the app's key window and insert the destination view above the current (source) one.
    let window = UIApplication.sharedApplication().keyWindow
    window?.insertSubview(secondVCView, aboveSubview: firstVCView)

    // Animate the transition.
    UIView.animateWithDuration(0.3, animations: { () -> Void in
        firstVCView.frame = CGRectOffset(firstVCView.frame, -screenWidth, 0.0)
        secondVCView.frame = CGRectOffset(secondVCView.frame, -screenWidth, 0.0)

    }) { (Finished) -> Void in
        self.sourceViewController.presentViewController(self.destinationViewController ,
                                                        animated: false,
                                                        completion: nil)
    }

1 Answers1

0

What you need to do is initially position the second view on the left of the first view rather than the right.

secondVCView.frame = CGRectMake(-screenWidth, 0.0, screenWidth, screenHeight)

Then in the animation you want to go in reverse:

firstVCView.frame = CGRectOffset(firstVCView.frame, screenWidth, 0.0)
secondVCView.frame = CGRectOffset(secondVCView.frame, screenWidth, 0.0)

So the end result:

let screenWidth = (UIScreen.mainScreen().bounds).width
let screenHeight = (UIScreen.mainScreen().bounds).height

let firstVCView = sourceViewController.view
let secondVCView = destinationViewController.view
secondVCView.frame = CGRectMake(-screenWidth, 0.0, screenWidth, screenHeight)

// Access the app's key window and insert the destination view above the current (source) one.
let window = UIApplication.sharedApplication().keyWindow
window?.insertSubview(secondVCView, aboveSubview: firstVCView)

// Animate the transition.
UIView.animateWithDuration(0.3, animations: { () -> Void in
    firstVCView.frame = CGRectOffset(firstVCView.frame, screenWidth, 0.0)
    secondVCView.frame = CGRectOffset(secondVCView.frame, screenWidth, 0.0)

}) { (Finished) -> Void in
    self.sourceViewController.presentViewController(self.destinationViewController ,
                                                    animated: false,
                                                    completion: nil)
}
Michael Hudson
  • 1,330
  • 4
  • 21
  • 25