首页 > 解决方案 > 如何以相同的速度滑动多个 UIView

问题描述

我的故事板中有两个视图,我通过以下代码使用高度约束来滑动它们。

滑动后高度为 150.0 的 ViewOne

       UIView.animate(withDuration: 0.3) {
            self.con_ViewOneHeight.constant = 150.0
            self.view.layoutIfNeeded()
        }

滑动后高度为 1000.0 的 ViewTwo

       UIView.animate(withDuration: 0.3) {
            self.con_ViewTwoHeight.constant = 1000.0
            self.view.layoutIfNeeded()
        }

两个动画都在 0.3 秒内发生,但第二个视图的动画发生的速度比第一个快。我知道它正在发生,因为时间是相同的,但是我们如何计算每个具有相同速度的视图的时间?

标签: iosswiftanimationuiviewuikit

解决方案


您应该以秒/点为单位设置所需的速度,然后计算每个高度的时间:

let speed = 0.3/150.0 // 0.3 seconds for 150 points
let heightForViewOne = 150.0 
let timeForViewOne = heightForViewOne * speed // (=0.3s)
let heightForViewTwo = 1000.0
let timeForViewTwo = heightForViewTwo * speed // (=2s)

然后在动画调用中使用 timeForViewxxx 和 heightForViewxxx


推荐阅读