2015-06-14 67 views
0

如何为不同的按钮设置动画效果?我提出在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

你是说一个接一个地向按钮添加动画吗?

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

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)