3

I have an IBOutlet Collection of buttons that I am trying to present on screen sequentially. They all start off screen fine, but as they animate in, I'd like each button to be presented on screen 0.05 seconds after the previous button. I can't figure out how to increment the delay in UIView.animateWithDuration. With the code below, they are all animating on screen at the same time.

//outlet collection
@IBOutlet var options: [UIButton]!

let increment = 0.25

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    for button in options {
        button.center.y += view.bounds.height
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    for button in options {
        UIView.animateWithDuration(1.0, delay: increment, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.0, options: nil, animations: {
            button.center.y -= self.view.bounds.height
            self.increment + 0.05
            }, completion: nil)
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Nacho
  • 33
  • 2

3 Answers3

1
for button in options {
        UIView.animateWithDuration(1.0, delay: increment, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.0, options: nil, animations: {
            button.center.y -= self.view.bounds.height
            }, completion: nil)

    }
           increment = increment + 0.05

}

Besides: Change this

let increment = 0.25

To

   var increment = 0.25

Increase the increment outside animation. Because animateWithDuration is an async method,it will return first. So,all your button have same delay.

Leo
  • 24,596
  • 11
  • 71
  • 92
  • Thanks for the input. For some reason it still isn't working. I moved increment outside of the animation as you suggested but all of the buttons are still animating on screen together. I did move everything over to viewDidAppear as well and tried it there but it's still not working. – Nacho May 06 '15 at 03:20
0
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!

@IBOutlet var options: [UIButton]!

let increment = 0.25


override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    for button in options {
        button.center.y += view.bounds.height

    }
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

        for button in options {
            UIView.animateWithDuration(1.0, delay: increment, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.0, options: nil, animations: {
                button.center.y -= self.view.bounds.height
                }, completion: nil)

            self.increment + 0.05
    }
}

Also getting error "Cannot invoke '+=' with an argument list of type '(Double, FloatLiteralConvertible)'" when using += but it will take just +

Nacho
  • 33
  • 2
  • See my updates,if you set a property as `let`, it can not be changed – Leo May 06 '15 at 05:26
  • You should edit your question, not add this as an answer - it is not an answer – jrturton May 06 '15 at 08:25
  • That worked! I kept thinking that I was doing something incorrect with my for-loop but completely missed the constant. Thanks! @jrturton, first time post, thanks for the heads up. – Nacho May 06 '15 at 23:38
-1

Here is the way to achieve the required delay between animations:

var i! as UInt64;
i = 0
for button in options {
                // your animation with delay
        UIView.animateWithDuration(1.0, delay: (i *0.05), usingSpringWithDamping: 0.7, initialSpringVelocity: 0.0, options: nil, animations: {
            button.center.y -= self.view.bounds.height
            }, completion: nil)
    })
    ++i

}
Vijay Masiwal
  • 1,153
  • 8
  • 12
  • There is a `delay` parameter right there in the animation method, why would you mess around with GCD for this? – jrturton May 06 '15 at 08:26