首页 文章

添加带动画的约束以进行查看

提问于
浏览
1

嘿我正试图通过改变它的约束来引入一个视图

UIView.animate(withDuration: 500, animations: {
        self.forgetTopConstraint.isActive = false
        self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self.forgetPasswordView, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))
    })

forgetTopConstraint 是一个约束,它将 forgetPasswordView 的顶部锚定到 view 的底部,并且常量为0,所以 forgetTopConstraint 不在视野中,然后我试图用它代码将其内部动画到 view 的中心,但它突然出现而不是动画

1 回答

  • 1

    没有看到更多的周围代码或视图设置,听起来你只需要在包含视图上调用 layoutIfNeeded . 这是实际执行应用更新约束的工作的方法 . layoutIfNeeded 由视图的许多其他更改自动触发,但在想要设置动画的情况下,您实际上不需要在动画块内调用任何其他布局更改 . 只需调用一个语句即可使其为以前的所有更改设置动画:

    self.forgetTopConstraint.isActive = false
    self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self.forgetPasswordView, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))
    UIView.animate(withDuration: 500, animations: {
        self.view.layoutIfNeeded()    
    })
    

相关问题