35
votes

How do I do a simple completion block in Swift 3?

I want to set self.isOpen = true in the completion of the animation:

            UIView.animate(withDuration: 0.25, delay: 0.0, options: [], animations: {
                self.isOpen = true
                self.drawerView?.frame = CGRect(x: 0, y: 0, width: (self.drawerView?.frame.size.width)!, height: (self.drawerView?.frame.size.height)!)
                self.contentView?.frame = CGRect(x: 200, y: 0, width: (self.contentView?.frame.size.width)!, height: (self.contentView?.frame.size.height)!)
            }, completion: nil)

In passing:

It's pretty impossible to learn Swift 3 atm due to NOTHING on the internet working :(


I've also searched this entire document for even a mention of the word "animate" and could not find anything:

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/index.html#//apple_ref/doc/uid/TP40014097-CH3-ID0

1
For the future: After auto-generating the .animate(...) function, double-click on the bluely highlighted ((Bool) -> Void)? editor placeholder and the structure of the anonymous block will prepare :)mmika1000

1 Answers

107
votes

You add it like this:

UIView.animate(withDuration: 0.25, delay: 0.0, options: [], animations: {
    self.drawerView?.frame = CGRect(x: 0, y: 0, width: (self.drawerView?.frame.size.width)!, height: (self.drawerView?.frame.size.height)!)
    self.contentView?.frame = CGRect(x: 200, y: 0, width: (self.contentView?.frame.size.width)!, height: (self.contentView?.frame.size.height)!)
}, completion: { (finished: Bool) in
    self.isOpen = true
})