iOS Segue - Left to Right -

前端 未结 11 922
旧巷少年郎
旧巷少年郎 2020-11-30 20:40

I\'ve read the other posts on segues but none solve my question.

Simply put, my ViewControllers are ordered, like a book. I want backward transitions (

相关标签:
11条回答
  • 2020-11-30 21:09

    This is how I achieve the effect without requiring a nav-controller. Try this instead:

    Swift 4:

    import UIKit
    class SegueFromLeft: 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, y: 0)
    
            UIView.animate(withDuration: 0.25,
                                  delay: 0.0,
                                options: .curveEaseInOut,
                             animations: {
                                    dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
                                    },
                            completion: { finished in
                                    src.present(dst, animated: false, completion: nil)
                                        }
                            )
        }
    }
    

    Swift 3:

    import UIKit
    
    class SegueFromLeft: UIStoryboardSegue
    {
        override func perform()
        {
            let src = self.sourceViewController
            let dst = self.destinationViewController
    
            src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
            dst.view.transform = CGAffineTransformMakeTranslation(-src.view.frame.size.width, 0)
    
            UIView.animateWithDuration(0.25,
                delay: 0.0,
                options: UIViewAnimationOptions.CurveEaseInOut,
                animations: {
                    dst.view.transform = CGAffineTransformMakeTranslation(0, 0)
                },
                completion: { finished in
                    src.presentViewController(dst, animated: false, completion: nil)
                }
            )
        }
    }
    

    Then in the storyboard, click on the segue you'd like to change. In the attributes inspector change the type to 'Custom' and change the class to 'SegueFromLeft'

    0 讨论(0)
  • 2020-11-30 21:15

    You can use of the predeclared type of transition in transitionWithView method

    UIView.transitionWithView(self.window!, duration: 0.5, options:.TransitionFlipFromLeft, animations: { () -> Void in
                self.window!.rootViewController = mainVC
                }, completion:nil)
    

    I guess .TransitionFlipFromLeft is the desired one

    To complete the task, drag a new view controller to your storyboard and name it with some id. This is going to be the destination of the transition.

    then instantiate this view controller from code

    let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
    let mainVC = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
    

    You can do this either inside the IBAction or for example in ViewDidLoad but probably IBAction will be the better choice. Pay attention to type the correct identifiers for both storyboard and view controller. Also, you have to declare your appDelegate instance. Here is the implemented IBAction

    @IBAction func push(sender: UIButton) {
    
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let mainVC = storyboard.instantiateViewControllerWithIdentifier("secondVC") as! UIViewController
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    
        UIView.transitionWithView(appDelegate.window!, duration: 0.5, options: .TransitionFlipFromLeft , animations: { () -> Void in
            appDelegate.window!.rootViewController = mainVC
            }, completion:nil)
    }
    

    If this is not what you have expected, probably you might want to make a custom animation. This article is really helpful : http://mathewsanders.com/animated-transitions-in-swift/

    0 讨论(0)
  • 2020-11-30 21:15

    Updated the accepted answer to Swift 4:

    class SegueFromLeft: 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, y: 0)
    
        UIView.animate(withDuration: 0.25,
                                   delay: 0.0,
                                   options: UIViewAnimationOptions.curveEaseInOut,
                                   animations: {
                                    dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
        },
                                   completion: { finished in
                                    src.present(dst, animated: false, completion: nil)
        })
         }
    }
    

    Feel free to copy this into the accepted answer and delete this comment. (It's a straight update, nothing new.)

    0 讨论(0)
  • 2020-11-30 21:21

    Accepted answer updated for Swift 3 (as of Jun 2017)

    Segue from left to right

    import UIKit
    
    class SegueFromLeft: UIStoryboardSegue {
        override func perform() {
            let src = self.source       //new enum
            let dst = self.destination  //new enum
    
            src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
            dst.view.transform = CGAffineTransform(translationX: -src.view.frame.size.width, y: 0) //Method call changed
            UIView.animate(withDuration: 0.25, delay: 0.0, options: UIViewAnimationOptions.curveEaseInOut, animations: { 
                dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
            }) { (finished) in
                src.present(dst, animated: false, completion: nil) //Method call changed
            }
        }
    }
    

    Segue from right to left

    import UIKit
    
    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: UIViewAnimationOptions.curveEaseInOut, animations: { 
                dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
            }) { (finished) in
                src.present(dst, animated: false, completion: nil)
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 21:23

    Updated accepted answer in Swift 3:

    import UIKit
    
    class SegueFromLeft: 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, y: 0)
    
            UIView.animate(withDuration: 0.25,
                delay: 0.0,
                options: UIViewAnimationOptions.curveEaseInOut,
                animations: {
                    dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
                },
                completion: { finished in
                    src.present(dst, animated: false, completion: nil)
                }
            )
        }
    }
    
    0 讨论(0)
  • 2020-11-30 21:24

    It sounds like you are just trying to pop a view controller off the UINavigationController's stack, just like the default back button would.

    You can do one of two things. The easiest is to connect your custom back button to an IBAction that calls popViewControllerAnimated():

    @IBAction func tappedCustomBackButton(sender: AnyObject) {
        self.navigationController?.popViewControllerAnimated(true)
    }
    

    Or you can create an unwind segue from your second view controller back to your first.

    0 讨论(0)
提交回复
热议问题