Animate with completion in WatchKit WKInterfaceController

The interface on WKInterfaceController only provides

public func animateWithDuration(duration: NSTimeInterval, animations: () -> Void)

But what if you want to chain animations with completion blocks like that we are used to in iOS?

Here's an extension of WKInterfaceController that can help you do that.

//delay is a global function that helps us make the code cleaner.

public func delay(delay: NSTimeInterval, closure:() -> ()) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), closure)
}


extension WKInterfaceController {
    
    public func animateWithDuration(duration: NSTimeInterval, animations: () -> Void, completion: () -> ()) {
        
        animateWithDuration(duration) {
                animations()
        }
        delay(duration) {
                completion()
        }
    }
}


You can implement it like this in WKInterfaceController:

animateWithDuration(0.5, animations: {

    //Animations

    }, completion: {

        //Completion

})