Chris Allinson Chris Allinson - 2 months ago 87
iOS Question

UIView.animate - Swift 3 - completion

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

Answer

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