37

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

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Chris Allinson
  • 1,837
  • 2
  • 26
  • 39
  • 2
    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 :) – emmics Aug 16 '17 at 15:30

1 Answers1

109

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
})
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thanks a bunch!!! Their "suggestion" was too cryptic to understand Actually what to write ... and the lack of documentation, and overwhelming amount of non-working swift2.2- code is quite frustrating :( – Chris Allinson Sep 25 '16 at 00:44