swift - 将不同的按钮动画到正确的位置

标签 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 )
}

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

最佳答案

你的意思是给按钮一一添加动画吗?

尝试一下这个功能,并配置延迟参数。

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/

相关文章:

ios - Swift 中的可选包装,为什么 Swift 将 "Optional"添加到字符串中

ios - 如何让谷歌在 swift 中放置标记纬度和经度

ios - Google 登录后继续

ios - 使用 CIFilter 对 Photoshop LUT 滤镜进行颜色校正

iOS - 自定义 View : Intrinsic content size ignored after updated in layoutSubviews()

mysql - Swift Vapor/Fluent 模型中的其他数据类型(例如 Unsigned SmallInt、Datetime 和 Decimal)?

ios - Alamofire:具有额外属性的可编码对象

ios - 无法使 mediaPlayer View 在 UIView 内水平旋转

ios - 使 Sprite 出现在随机的 y 点

ios - 如何使用 Swift 更改 iOS 8 自定义键盘高度?