Search code examples
swiftsegueuiviewanimationuistoryboardsegueuiviewanimationtransition

How to fix `prepareForSegue()` override `UIViewAnimation`


I have MainViewController which has stack view and its width is over screen's width. There is slide out menu at left side of storyboard. And there is Menu button on the screen which triggers an animation to changes UIStackView's from -260 to 0. But when I click some button at slide out menu, prepareForSegue() overrides the animation and content changes immediately. How can fix that?

You can find a gif below.

Here is the codes:

 class MainViewController: UIViewController
{
    var container: ContainerViewController?
    @IBOutlet weak var superView: UIStackView!

    @IBAction func menuButtonsChangeContent(sender: AnyObject) {
        switch(sender.tag){
        case 1:
            container?.changeContent("first")
        case 2:
            container?.changeContent("second")
        case 3:
            container?.changeContent("third")
        default:
            break
        }
    }


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "Container"{
            self.slideOutMenu("")
            container = segue.destinationViewController as? ContainerViewController
        }
    }

    @IBAction func slideOutMenu(sender: AnyObject) {

        if self.superView.frame.origin.x != -260 {

            UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0.5, options: .CurveEaseInOut, animations: ({

                self.superView.frame = CGRect(x: -260, y: 20, width: self.superView.frame.width, height: self.superView.frame.height)

            }), completion: nil)
        }else{
            UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0.5, options: .CurveEaseInOut, animations: ({
                self.superView.frame = CGRect(x: 0, y: 20, width: self.superView.frame.width, height: self.superView.frame.height)

            }), completion: nil)
        }
    }
}

//ContainerViewController.swift       
 class ContainerViewController: UIViewController 
 {
    var sourceVC: UIViewController?
    var destinationVC: UIViewController?
    var segueIdentifier: String?
    var counter = 0

    func changeContent(segueIdentifier: String)
    {
        self.performSegueWithIdentifier(segueIdentifier, sender: self)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0.5, options: .CurveEaseInOut, animations: ({

            if self.sourceVC != nil {
                self.sourceVC?.view.removeFromSuperview()
                print("deleted")
            }

            self.destinationVC = segue.destinationViewController
            self.addChildViewController(self.destinationVC!)
            self.destinationVC?.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
            self.view.addSubview((self.destinationVC?.view)!)
            self.destinationVC?.didMoveToParentViewController(self)
            self.sourceVC = self.destinationVC
        }), completion: nil)
    }
}

And Storyboard:

enter image description here

GIF:

enter image description here


Solution

  • I was using a UIStackView as super view in MainViewController but an issue about shadows came up and I changed UIStackView to UIView. Maybe it is weird but the problem is gone.

    enter image description here