iOS Segue - Left to Right -

前端 未结 11 923
旧巷少年郎
旧巷少年郎 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:28

    Here is a custom-segue class you can use to perform a left-to-right segue in Swift. It requires QuartzCore framework and minimal animation.

    Swift 2.2

    import UIKit
    import QuartzCore
    
    class SegueFromLeft: UIStoryboardSegue {
    
        override func perform() {
            let src: UIViewController = self.sourceViewController
            let dst: UIViewController = self.destinationViewController
            let transition: CATransition = CATransition()
            let timeFunc : CAMediaTimingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
            transition.duration = 0.25
            transition.timingFunction = timeFunc
            transition.type = kCATransitionPush
            transition.subtype = kCATransitionFromLeft
            src.navigationController!.view.layer.addAnimation(transition, forKey: kCATransition)
            src.navigationController!.pushViewController(dst, animated: false)
        }
    
    }
    

    Swift 3.0

    import UIKit
    import QuartzCore
    
    class SegueFromLeft: UIStoryboardSegue {
    
        override func perform() {
            let src: UIViewController = self.source
            let dst: UIViewController = self.destination
            let transition: CATransition = CATransition()
            let timeFunc : CAMediaTimingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
            transition.duration = 0.25
            transition.timingFunction = timeFunc
            transition.type = kCATransitionPush
            transition.subtype = kCATransitionFromLeft
            src.navigationController!.view.layer.add(transition, forKey: kCATransition)
            src.navigationController!.pushViewController(dst, animated: false)
        }
    }
    

    The "Back" button will still appear in the navigation bar on the transitioned view controller, but you can easily disable/configure the navigation controller for that view controller.

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

    To create a "back button" like animation, use this.

    class SegueRightToLeft: 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/2, y: 0) 
        //slice the x axis translation in half
    
        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:33

    In my case I used to make a sidebar menu...

    I created a new view controller and two customs segues

    TO OPEN THE MENU:

    import Foundation
    import UIKit
    
    class SegueFromLeft: UIStoryboardSegue {
    
      override func perform() {
    
        let src = self.source as UIViewController
        let dst = self.destination as UIViewController
    
        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)
        }
        )
    
    }
    
    }
    

    TO CLOSE THE MENU:

    import Foundation
    import UIKit
    
    class SegueFromRight: UIStoryboardSegue {
    
    override func perform() {
    
        let src = self.source as UIViewController
        let dst = self.destination as UIViewController
    
        src.view.superview?.insertSubview(dst.view, belowSubview: src.view)
        src.view.transform = CGAffineTransform(translationX: 0, y: 0)
    
        UIView.animate(withDuration: 0.25,
                                   delay: 0.0,
                                   options: UIViewAnimationOptions.curveEaseInOut,
                                   animations: {
                                    src.view.transform = CGAffineTransform(translationX: -src.view.frame.size.width, y: 0)
        },
                                   completion: { finished in
                                    src.dismiss(animated: false, completion: nil)
        }
        )
    }
    }
    

    I hope it helped you...

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

    Hey guy I have complete solution just copy and past this code written swift 3.

    func menu(){
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "MyAccountViewController") as! MyAccountViewController
    
        let transition: CATransition = CATransition()
        let timeFunc : CAMediaTimingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        transition.duration = 0.5
        transition.timingFunction = timeFunc
        transition.type = kCATransitionPush
        transition.subtype = kCATransitionFromLeft
        self.navigationController?.view.layer.add(transition, forKey: kCATransition)
        self.navigationController?.pushViewController(vc, animated: false)
    }
    

    Note: Change the name of your ViewController with "MyAccountViewController" text.

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

    Segue from right.You can override the perform function in segue like below. put this function inside a custom segue class and assign this class to the segue. it will work for both controllers with navigation controller and without navigation controller

    override func perform()
    {
        let src = self.sourceViewController
        print(src)
        let dst = self.destinationViewController
        print(dst)
    
        src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
        dst.view.transform = CGAffineTransformMakeTranslation(src.view.frame.size.height, 0)
    
        UIView.animateWithDuration(0.35,
                                   delay: 0.0,
                                   options: UIViewAnimationOptions.CurveEaseInOut,
                                   animations: {
                                    dst.view.transform = CGAffineTransformMakeTranslation(0, 0)
            },
                                   completion: { finished in
                                    if let navController = src.navigationController {
                                        navController.pushViewController(dst, animated: false)
    
                                    } else {
                                        src.presentViewController(dst, animated: false, completion: nil)
                                    }            }
        )
    }
    

    if you want the segue from left then use this

    CGAffineTransformMakeTranslation(-src.view.frame.size.height, 0)
    
    0 讨论(0)
提交回复
热议问题