我希望这些元素在持续时间内在一行中逐个制作动画,但它们是在同一时刻一起制作动画
authClicked = true
var angle: CGFloat = 45.0
google = UIButton(type: .custom)
facebook = UIButton(type: .custom)
vk = UIButton(type: .custom)
instagram = UIButton(type: .custom)
regular = UIButton(type: .custom)
let buttonArray = [self.google: "Google", self.facebook: "Facebook", self.vk: "VK", self.instagram: "Instagram", self.regular: "Regular"];
for (button, buttonName) in buttonArray {
button.frame = CGRect(x: x, y: y, width: 150, height: 150)
button.addTarget(self, action: #selector(self.buttonClicked), for: .touchUpInside)
button.setTitle(buttonName, for: .normal)
button.alpha = 0
circleButton(button: button)
angle -= 45.0
view.addSubview(button)
let cosX = cos(angle) * 200
let sinY = sin(angle) * 200
UIView.animate(withDuration: 1.0, delay: 1.0, options: [], animations: {
button.transform = CGAffineTransform(translationX: cosX, y: sinY).concatenating(CGAffineTransform(scaleX: 0.5, y: 0.5))
button.alpha = 1
}) { (done) in
print(done)
}
最佳答案
根据代码,它们都是同时执行的(延迟1.0秒后)。
要更改此设置:
// create a variable outside of the for loop
var animationDelayTime = 1.0
// the for loop
for (button, buttonName) in buttonArray {
// the other stuff
...
...
...
// instead of 1.0 for delay, use animationDelayTime
UIView.animate(withDuration: 1.0, delay: animationDelayTime, options: [], animations: {
button.transform = CGAffineTransform(translationX: cosX, y: sinY).concatenating(CGAffineTransform(scaleX: 0.5, y: 0.5))
button.alpha = 1
}) { (done) in
print(done)
}
// after the animation is done, increment the var for the next iteration.
// it will be 1.25 for the 2nd loop, then 1.50, then 1.75, etc.
animationDelayTime += 0.25
} // end, for loop
关于ios - UIView 使用少量对象进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50432809/