【问题标题】:Trying to animate a UIView height from the bottom up, instead of the top down尝试从下向上而不是自上而下为 UIView 高度设置动画
【发布时间】: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


【解决方案1】:

iOS 视图的原点是屏幕的左上角。视图的框架将原点固定在左上角,高度表明它向下延伸的距离。

如果增加视图的高度,它的顶部不会移动 - 它的底部会向下延伸。

如果您希望视图看起来向上增长,则需要增加高度,同时将 origin.y 减少相同的量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    相关资源
    最近更新 更多