【发布时间】:2020-05-11 21:25:13
【问题描述】:
在 Swift 中,我希望对UIView.animate 进行多次调用。也就是说,当一个动画完成时,我希望另一个动画在之后继续,依此类推。
对UIView.animate 的调用有一个Trailing Closure,我目前正在使用它来对UIView.animate 进行第二次调用。
问题是:我想做N个单独的动画
来自UIView.animate 的 Apple 文档
completion动画序列结束时要执行的块对象。此块没有返回值,并采用单个布尔参数,指示动画是否在调用完成处理程序之前实际完成。如果动画的持续时间为 0,则在下一个 run loop 循环开始时执行此块。该参数可能为NULL。
理想情况下,我想遍历一组动画durations 并在对animate()的调用中使用
例如,
目标
遍历一个数组并为每个动画应用这些参数
let duration = [3.0, 5.0, 10.0]let alpha = [0.1, 0.5, 0.66]- Xcode 版本 11.4.1 (11E503a)
我尝试过的
使用map 进行迭代,并且????希望它有效
- 问题是只出现最终动画。所以系列中没有任何内容
- 研究问:我是否需要在
UIView中设置一个布尔值,表示动画是连续发生的?
let redBox = UIView()
redBox.backgroundColor = UIColor.red
self.view.addSubview(redBox)
let iterateToAnimate = duration.enumerated().map { (index, element) -> Double in
print(index, element, duration[index])
UIView.animate(withDuration: duration[index], // set duration from the array
animations: { () in
redBox.alpha = alpha[index]
}, completion:{(Bool) in
print("red box has faded out")
})
}
如何制作一个动画
let redBox = UIView()
redBox.backgroundColor = UIColor.red
self.view.addSubview(redBox)
// One iteration of Animation
UIView.animate(withDuration: 1,
animations: { () in
redBox.alpha = 0
}, completion:{(Bool) in
print("red box has faded out")
})
链接两个animate 迭代的丑陋方式(希望避免)
// two iterations of Animation, using a trailing closure
UIView.animate(withDuration: 1,
animations: { () in
redBox.alpha = 0
}, completion:{(Bool) in
print("red box has faded out")
}) { _ in // after first animation finishes, call another in a trailing closure
UIView.animate(withDuration: 1,
animations: { () in
redBox.alpha = 0.75
}, completion:{(Bool) in
print("red box has faded out")
}) // 2nd animation
}
【问题讨论】:
标签: ios swift animation closures uiviewanimation