本文介绍了animateWithDuration 缺少完成块,WatchOS 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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.

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

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

这篇关于animateWithDuration 缺少完成块,WatchOS 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 05:44