gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:07:26 26 4
gpt4 key购买 nike

我正在尝试使用 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 位置设置不正确?

最佳答案

如果您尝试使用约束来制作动画,您应该改为更改约束,然后在父 View 的动画 block 中调用 layoutIfNeeded

1) 在动画 block 外设置新的约束

2) 在动画 block 中,在父 View 上调用 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()
})
}

关于ios - Swift UIView 框架未在 animateWithDuration 内正确更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32314585/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com