我正在尝试将 UIView 的高度从 1px 的高度设置为 41px 的高度。它现在有点工作,但不是从底部到顶部动画 View ,而是从顶部到底部。
这是我用来使它上下动画的代码
func animateBackgroundHeightUp() {
print("animate")
UIView.animate(withDuration: 0.5, animations: {
self.personBg.frame.size.height = 41
})
}
func animateBackgroundHeightDown() {
print("animate")
UIView.animate(withDuration: 0.5, animations: {
self.personBg.frame.size.height = 1
})
}
我想我必须弄清楚如何设置原点 y,但我似乎找不到 swift 3 示例。
编辑
func animateBackgroundHeightUp() { print("animate") UIView.animate(withDuration: 0.5, animations: { self.personBg.frame.size.height = 41 self.personBg.frame.origin.y = -41 }) }
func animateBackgroundHeightDown() { print("animate") UIView.animate(withDuration: 0.5, animations: { self.personBg.frame.size.height = 1 self.personBg.frame.origin.y = 41 }) }
最终为我工作的是这个配置。但我很困惑为什么原点需要是 20?
func animateBackgroundHeightUp() {
print("animate")
UIView.animate(withDuration: 0.25, animations: {
self.personBg.frame.size.height = 41
self.personBg.frame.origin.y = 20
})
}
func animateBackgroundHeightDown() {
print("animate")
UIView.animate(withDuration: 0.25, animations: {
self.personBg.frame.size.height = 1
self.personBg.frame.origin.y = 59.9
})
}
59.9 的数字是通过在加载时打印出原点 y 的值来计算的。
最佳答案
iOS View 的原点是屏幕的左上角。 View 的框架将原点固定在左上角,高度表示它向下延伸的距离。
如果增加 View 的高度,它的顶部不会移动 - 它的底部会向下延伸。
如果您希望 View 看起来向上增长,则需要增加高度,同时将 origin.y 减少相同的量。
关于ios - 尝试从下向上而不是自上而下为 UIView 高度设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41653693/