animateWithDuration缺少完成块,WatchOS 2

Rub*_*Jr. 5 animatewithduration apple-watch watchkit watchos watchos-2

watchOS 2在其animateWithDuration函数中没有任何类型的完成块.我正在开发一款需要在动画完成后运行代码的游戏.有没有工作?也许使用键值观察?另一种方法是使用与动画长度相匹配的计时器,但由于显而易见的原因,这是非理想的.

Pun*_*thi 6

NSOperation对我也不起作用.我现在只是使用以下解决方案,直到Apple正式添加完成块.不理想,但它的工作原理.我打开了雷达.也许Apple会在watchOS 2的未来种子中添加一个完成块.

此扩展添加animateWithDuration方法,该方法采用完成块.并且在动画块的持续时间之后调用完成块.

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?()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)