如何为不同的按钮设置动画?我在 for 循环中制作了 16 个按钮:
for i in 1..<17
{
if(i == 5 || i == 9 || i == 13){
added = 0
moveToBottom += 60
}
var button = UIButton(frame: CGRectMake(CGFloat(Int(middleWidth) + added), CGFloat(moveToBottom), 50, 50))
self.view.addSubview(button)
UIView.animateWithDuration(2, animations:{
button.frame = CGRectMake(CGFloat(Int(middleWidth) + added), CGFloat(Int(middleHeight) + moveToBottom), 50, 50)
},completion: nil )
}
我将它们设置为动画到正确的位置,但我得到的是所有按钮都在动画的同一时间执行。这不是我想要的,按钮必须在不同的时间动画到其位置,我该怎么做?
最佳答案
你的意思是给按钮一一添加动画吗?
尝试一下这个功能,并配置延迟参数。
UIView.animateWithDuration(duration: NSTimeInterval, delay: NSTimeInterval, options: UIViewAnimationOptions, animations: { () -> Void in
// your animate code...
}, completion:
// completion block ....
)
根据您的情况,写成这样:
UIView.animateWithDuration(2, delay: 2*(i-1), options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
button.frame = CGRectMake(CGFloat(Int(middleWidth) + added), CGFloat(Int(middleHeight) + moveToBottom), 50, 50)
}, completion: nil)
关于swift - 将不同的按钮动画到正确的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30830187/