从Apple Documents pausesoncompletion
因为当此属性为true时未调用完成处理程序,所以您不能使用动画制作者的完成处理程序来确定动画何时完成运行。 相反,您可以通过观察isRunning属性来确定动画何时结束。
但是我发现观察isRunning
不起作用。 util i wathched WWDC 2017 - Session 230-Advanced Animations with UIKit,我知道我应该遵守running
//not work
animator.addObserver(self, forKeyPath: "isRunning", options: [.new], context: nil)
//this work
animator.addObserver(self, forKeyPath: "running", options: [.new], context: nil)
我的问题是:不仅在这种情况下,在哪里可以找到出色的密钥路径。谢谢
最佳答案
在Swift中,建议使用基于块的KVO API(从Swift 4开始可用),该API允许您以类型安全和编译时检查的方式观察属性:
// deinit or invalidate the returned observation token to stop observing
let observationToken = animator.observe(\.isRunning) { animator, change in
// Check `change.newValue` for the new (optional) value
}
请注意,键路径是
\.isRunning
,因为Swift中UIViewPropertyAnimator
的属性称为isRunning
。两者的好处是您不必知道给定属性的字符串是如何拼写的,并且更改后的值与观察到的属性具有相同的类型。
请注意,在Objective-C中此API不可用,因此the corresponding Objective-C documentation要求您观察“运行”属性。这是因为在Objective-C中,该属性称为“running”(但具有名为“isRunning”的吸气剂)。