Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

animateWithDuration lacks completion block, WatchOS 2

watchOS 2 does not have any kind of completion block in its animateWithDuration function. I'm working on a game that requires running code after an animation is completed. Are there any work arounds? Perhaps using key-value observation? The alternative would be to use a timer that matches up with the length of the animation but that's non-ideal for obvious reasons.

like image 796
Ruben Martinez Jr. Avatar asked Jun 13 '15 22:06

Ruben Martinez Jr.


1 Answers

NSOperation didn't work for me too. I am just using the following solution for now until Apple officially adds a completion block. Not ideal but it works. I have opened a Radar. Maybe Apple would add a completion block in a future seed of watchOS 2.

This extension adds an animateWithDuration method that takes a completion block. And the completion block is called after the duration of the animation block.

extension WKInterfaceController {
    func animateWithDuration(duration: NSTimeInterval, animations: () -> Void, completion: (() -> Void)?) {
        animateWithDuration(duration, animations: animations)
        let completionDelay = dispatch_time(DISPATCH_TIME_NOW, Int64(duration * Double(NSEC_PER_SEC)))
        dispatch_after(completionDelay, dispatch_get_main_queue()) {
            completion?()
        }
    }
}
like image 86
Puneet Sethi Avatar answered Oct 04 '22 13:10

Puneet Sethi