Swift UIView 框架未在 animateWithDuration 内正确更新

Swift UIView frame not properly updating within animateWithDuration

我正在尝试使用 animateWithDuration 更新 UIView 的位置。当动画开始出现时,UIView 开始更新,但随后完成使其跳转到正确的位置。这告诉我 UIView 的框架设置没有正确设置,但我不明白为什么。这是代码:

func showInfoView() {

    infoView.hidden = false
    let newPosition = self.view.frame.height - (self.infoView.frame.height + 260)

    UIView.animateWithDuration(2.0, animations: {
        // Set the view to have the new y position
        self.infoView.frame.origin.y = newPosition
        }, completion: { finished in

            // Remove previous constraint
            if (self.hintConstraint != nil) {
                self.view.removeConstraint(self.hintConstraint)
            }

            // Create the new constraint
            self.hintConstraint = NSLayoutConstraint(item: self.infoView, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: newPosition)
            self.view.addConstraint(self.hintConstraint)
    })
}

谁能帮我理解为什么代码中的 y 位置设置不正确?

如果您尝试使用约束进行动画处理,您应该改为更改约束,然后在父视图的动画块中调用 layoutIfNeeded

1) 在动画块外设置新约束

2) 在动画块中,在父视图上调用 layoutIfNeeded

func showInfoView() {

    infoView.hidden = false
    let newPosition = self.view.frame.height - (self.infoView.frame.height + 260)
    self.hintConstraint = NSLayoutConstraint(item: self.infoView, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: newPosition)

    UIView.animateWithDuration(2.0, animations: {
        self.view.layoutIfNeeded()
    })
}