watchOS 2在其animateWithDuration函数中没有任何类型的完成块。我正在开发一款需要在动画完成后运行代码的游戏。有没有工作?也许使用键值观察?另一种方法是使用一个与动画长度相匹配的计时器,但由于显而易见的原因,它是非理想的。
答案 0 :(得分:6)
NSOperation对我也不起作用。我现在只是使用以下解决方案,直到Apple正式添加完成块。不理想,但它的工作原理。我打开了雷达。也许Apple会在watchOS 2的未来种子中添加一个完成块。
此扩展添加animateWithDuration方法,该方法采用完成块。并且在动画块的持续时间之后调用完成块。
TIdMessage.Sender
答案 1 :(得分:2)
尝试使用WKInterfaceController中的完成块扩展 animateWithDuration 方法
Obj-C版本(感谢@PunnetSethi):
- (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(void))completion{
[self animateWithDuration:duration animations:animations];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), completion);
}
使用WatchOS2处理真实的Watch
答案 2 :(得分:0)
Swift 4
watchOS 的高级 animate()方法:
import Foundation
import WatchKit
protocol Animatable: class {
func animate(forKey key: String,duration: TimeInterval, repeatCount count: CGFloat, autoreverses: Bool, animations: @escaping () -> Void, reverseAnimations: (() -> Void)?, completion: (() -> Void)?)
func stopAnimations()
}
extension Animatable {
func animate(forKey key: String, duration: TimeInterval, repeatCount count: CGFloat, autoreverses: Bool, animations: @escaping () -> Void, reverseAnimations: (() -> Void)?, completion: (() -> Void)?) {}
func stopAnimations() {}
}
extension WKInterfaceController: Animatable {
private struct AssociatedKeys {
static var animsDesc = "_animsDesc"
static var stopDesc = "_stopDesc"
}
var animations: [String: (() -> Void)?] {
get {return objc_getAssociatedObject(self, &AssociatedKeys.animsDesc) as? [String: (() -> Void)?] ?? [:]}
set {objc_setAssociatedObject(self, &AssociatedKeys.animsDesc, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)}
}
var animationStopStates: [String: Bool] {
get {return objc_getAssociatedObject(self, &AssociatedKeys.stopDesc) as? [String: Bool] ?? [:]}
set {objc_setAssociatedObject(self, &AssociatedKeys.stopDesc, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)}
}
func animate(forKey key: String, duration: TimeInterval, repeatCount count: CGFloat = 1.0, autoreverses: Bool = false, animations: @escaping () -> Void, reverseAnimations: (() -> Void)? = nil, completion: (() -> Void)? = nil) {
let isStopped = self.animationStopStates[key] ?? false
if isStopped {
completion?()
return
}
self.setAnimations(key, reverse: reverseAnimations)
let count = count - 1
let deadline = DispatchTime.now() + duration
self.animate(withDuration: duration, animations: animations)
DispatchQueue.main.asyncAfter(deadline: deadline) {
var deadline2 = DispatchTime.now()
if autoreverses, let rev = reverseAnimations {
self.animate(withDuration: duration, animations: rev)
deadline2 = DispatchTime.now() + duration
}
DispatchQueue.main.asyncAfter(deadline: deadline2, execute: {
if !count.isEqual(to: .infinity) && count <= 0 {
completion?()
} else {
self.animate(forKey: key, duration: duration, repeatCount: CGFloat(count), autoreverses: autoreverses, animations: animations, reverseAnimations: reverseAnimations, completion: completion)
}
})
}
}
/// Stops all the currently playing animations
func stopAnimations() {
for key in self.animations.keys {
guard let rev = self.animations[key] else {return}
self.animationStopStates[key] = true
self.animate(forKey: key, duration: 0, repeatCount: 0, autoreverses: false, animations: {}, reverseAnimations: rev)
}
self.animations.removeAll()
}
private func setAnimations(_ key: String, reverse: (() -> Void)?) {
if self.animations[key] == nil {
self.animations[key] = reverse
}
}
}
使用方法:
self.animate(forKey: "custom_anim1", duration: 1.0, repeatCount: .infinity, autoreverses: true, animations: {[unowned self] in
self.myButton.setHeight(100)
}, reverseAnimations: { [unowned self] in
self.myButton.setHeight(120)
}) {
print("Animation stopped!")
}
停止所有动画:
self.stopAnimations()
答案 3 :(得分:0)
您可以使用此扩展名。这是一个更好的解决方案,因为它不像延迟动画那样在与动画相同的时间后分派代码块,而是依赖时间。而且很优雅。
extension WKInterfaceController {
func animate(withDuration duration: TimeInterval,
animations: @escaping () -> Swift.Void,
completion: @escaping () -> Swift.Void) {
let queue = DispatchGroup()
queue.enter()
let action = {
animations()
queue.leave()
}
self.animate(withDuration: duration, animations: action)
queue.notify(queue: .main, execute: completion)
}
}
您可以轻松地使用它:
self.animate(withDuration: 0.2, animations: {
//your animation code
}, completion: {
//to do after you animation
})