动画子视图控制器视图

时间:2019-12-14 18:47:17

标签: ios swift

我有一个子视图控制器,我想在父视图控制器上添加一个按钮。当单击按钮时,我希望子VC从屏幕底部显示到其一半。 我试图设置高度限制的动画,但是由于某种原因,它不会从屏幕底部开始。所以动画看起来很奇怪。它从底部开始,然后向上和向下直到达到我指定的恒定值。 这就是我所拥有的:

    func setChildVC() {
    self.addChild(childVC)
    mainView.addSubview(childVC.view)

    childVC.didMove(toParent: self)
    childVC.view.translatesAutoresizingMaskIntoConstraints = false

    heightAnchor =             childVC.view.heightAnchor.constraint(equalToConstant: 0)

    NSLayoutConstraint.activate([
        childVC.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
        childVC.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
        childVC.view.bottomAnchor.constraint(equalTo: mainView.bottomAnchor),
        heightAnchor,
    ])
}

这就是我设置高度动画的方式(当前在viewDidLoad上):

self.heightAnchor.constant = 350
UIView.animate(withDuration: 2) {
        self.childVC.view.layoutIfNeeded()
}

我试图将childVC.view的y轴设置在屏幕下方,但是由于种种限制,它无法正常工作。

1 个答案:

答案 0 :(得分:1)

替换

self.childVC.view.layoutIfNeeded()

使用

self.mainView.layoutIfNeeded()

要正确测试它,请不要在viewDidLoad内尝试,为时过早,也出于屏幕高度不同的考虑

self.heightAnchor.constant = self.view.frame.height / 2.0