我正在使用 Xcode 快速制作一个应用程序,但我在处理某些按钮的动画时遇到了问题。 这是我的情况:
我在屏幕中间有三个按钮,我也在其中添加了一些约束。
我需要的是当按钮被点击时它到达屏幕的顶部,就像第二张带有动画的照片:
我已经试过了:
UIView.animateWithDuration(1, delay: 0, options: [], animations: {
self.Button2.center.y = 18 + self.Button2.frame.height/2
}, completion: nil)
但是发生的事情是按钮从屏幕底部滑到第一张照片的位置。它不尊重我在动画中写的位置。这可能是由于限制造成的吗?因为我将它垂直和水平居中。
你能帮我让它工作吗?
最佳答案
当您使用autolayout 和constraints 时,您永远不会更改动画中的按钮框架,就像您的代码一样:您必须始终使用约束。
这是一个通用示例:
myButtonCenterYConstraint.constant = 200.0
myButton.layoutIfNeeded()
UIView.animateWithDuration(Double(0.5), animations: {
myButtonCenterYConstraint.constant = 0
myButton.layoutIfNeeded()
})
因此,第一步是将要更改的按钮约束声明为 IBOutlets:
关于ios - 按钮动画无法在 swift 中正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38048351/