【发布时间】:2017-01-14 18:49:51
【问题描述】:
我正在尝试将 UIView 的高度从 1px 的高度设置为 41px 的高度。它现在有点工作,但不是从底部到顶部动画视图,而是从顶部到底部。
这是我用来让它上下动画的代码
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 的值来计算的。
【问题讨论】:
-
您应该避免使用小数位置,因为这会导致渲染引擎进行插值,并使位于非整数位置的视图看起来模糊。 (您可以在 Retina 设备上使用以 0.5 结尾的 x 和 y 位置,因为在 Retina 设备上每个点有 2 个像素。)
标签: ios xcode animation uiview swift3