将不同的按钮设置为正确的位置

时间:2015-06-14 13:52:55

标签: swift

我如何设置不同按钮的动画?我在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 )
}

我将它们设置为正确的位置,但我得到的是所有按钮与动画完全相同。这不是我想要的,按钮必须在不同的时间动画到它的位置,我该怎么做?

1 个答案:

答案 0 :(得分:1)

你的意思是逐个向按钮添加动画吗?

尝试此功能,并配置延迟参数。

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)