ios - 尝试从下向上而不是自上而下为 UIView 高度设置动画

标签 ios xcode animation uiview swift3

我正在尝试将 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/

相关文章:

ios - 将数据传递给另一个 View Controller

objective-c - Lion 不会运行使用 Mountain Lion 构建的应用程序

unity-game-engine - Unity - 如何在运行时更改玩家 Sprite 表?

html - 动画发生时元素不移动

swift - UISearchController 在 iOS 11 Swift 4 上隐藏状态栏

jquery - 带有 iPhone 动画曲线的动画 GUI

ios - Xcode 横向多个 Storyboard

iOS - UIScrollView 从半屏到全屏

ios - 如何在 swift 3.0 中将图像写入路径

ios - iTunes Connect 提交审核错误