【问题标题】:Animating View every time the View Controller appears?每次视图控制器出现时动画视图?
【发布时间】:2017-08-12 18:49:22
【问题描述】:

我有一个UIViewController,每次点击UITabBarButtonItem 时都会触发它。问题是它有点像菜单,由一个从屏幕底部向上滑动的UIView 组成。

现在,最初,底部约束设置为 -500,因此它不在屏幕上。我创建了一个函数,通过一些动画将约束设置回 100。问题是我认为如果我在viewWillAppear() 中调用该函数,它每次都会被动画化。但事实并非如此。我还尝试在viewDidDisappear() 中将约束重置回-500,以便在下次触发之前它在屏幕外。

不起作用。它只是第一次工作。我错过了一些东西,或者我在错误的地方调用它......

    class PostVC: UIViewController {

    @IBOutlet weak var menuBottomConstraint: NSLayoutConstraint!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        animateMenu()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        menuBottomConstraint.constant = -500
    }

    func animateMenu() {
        menuBottomConstraint.constant = 100
        UIView.animate(
            withDuration: 1.0,
            delay: 0.0,
            usingSpringWithDamping: 0.5,
            initialSpringVelocity: 0.0, options: .curveEaseOut,
            animations: {
                self.view.layoutIfNeeded()
            },
            completion: nil
        )
    }
}

【问题讨论】:

  • 不相关,但在调用super.viewWill[Disa|A]ppear 时不应硬编码true。传递animated 参数。

标签: swift uiview uiviewanimation viewwillappear


【解决方案1】:

每次更改Constraintconstant 值并看到效果时,必须调用layoutIfNeeded() 方法。

在您的代码中:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    menuBottomConstraint.constant = -500
    self.view.layoutIfNeeded()
}

我还建议用viewDidAppear替换viewWillAppear函数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多