Need help adding parameter to UIView.animateWithDuration in swift

Thanks in advance for any help. I have a collection of IBOutlet buttons that I am trying to present on the screen sequentially. They all start out fine, but when they come to life, I would like each button to appear on the screen 0.05 seconds after the previous button. I cannot figure out how to increase the latency in UIView.animateWithDuration. With the code below, they all animate the 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)

    }

}

      

Thanks again for any help!

+3


source to share


3 answers


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

}

      

Also: Change this

let increment = 0.25

      



For

   var increment = 0.25

      

Increase the outer animation increment

. Since animateWithDuration

this is an asynchronous method, it will be returned first . So, all of your buttons have the same delay.

0


source


@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 the error "Cannot call" + = "with argument list like" (Double, FloatLiteralConvertible) "when using + =, but it only takes +

0


source


Here's a 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

}

      

-1


source







All Articles