如何修复`prepareForSegue()`覆盖`UIViewAnimation`

时间:2015-12-13 17:14:06

标签: swift segue uiviewanimation uistoryboardsegue uiviewanimationtransition

我有MainViewController,它有堆栈视图,宽度超过屏幕宽度。 storyboard左侧有滑出菜单。屏幕上有菜单按钮,触发动画将UIStackView从-260更改为0.但是当我在滑出菜单中单击某个按钮时,prepareForSegue()会立即覆盖动画和内容更改。怎么解决这个问题?

你可以在下面找到一个GIF。

以下是代码:

 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)
    }
}

和故事板:

enter image description here

GIF:

enter image description here

1 个答案:

答案 0 :(得分:0)

我在UIStackView中使用MainViewController作为超级视图,但出现了关于阴影的问题,我将UIStackView更改为UIView。也许这很奇怪,但问题已经消失。

enter image description here