ios - 如何以相同的速度滑动多个 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 秒内发生,但第二个视图的动画发生的速度比第一个快。我知道它正在发生,因为时间是相同的,但是我们如何计算每个具有相同速度的视图的时间?
解决方案
您应该以秒/点为单位设置所需的速度,然后计算每个高度的时间:
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
推荐阅读
- javascript - 用 span 替换每个字符
- image - 保存图像的最佳方法是什么?作为实际图像还是base64?
- file - 在如何将类导入单独的文件中需要帮助
- php - PHP 生成器返回类型
- php - 在构建步骤中无法连接到 docker 容器“php”中的 mysql
- javascript - 添加 ”!” 到他们拥有的每件物品的末尾
- angular - 在 Angular 组件中捕获服务属性更改
- php - IOS中的Wordpress wp_logout_url()给出弹出框要求登录
- haskell - 使用类型类将值与 Haskell 中的类型相关联
- c# - 插入查询未在数据库表中插入会话 ID