观察 UIViewPropertyAnimator 是 运行 个问题

Observing UIViewPropertyAnimator is running issue

来自 Apple 文档 pausesoncompletion

Because the completion handler is not called when this property is true, you cannot use the animator's completion handler to determine when the animations have finished running. Instead, you determine when the animation has ended by observing the isRunning property.

但我发现观察 isRunning 不起作用。使用我 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)

我的问题是:我在哪里可以找到 excatly keypath,不仅仅是这种情况。谢谢~

在 Swift 中,建议使用基于块的 KVO API(从 Swift 4 开始可用),它允许您在类型中观察 属性安全和编译时检查方式:

// 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 ask you to observe the "running" property. This is because in Objective-C the property is called "running"(但有一个名为 "isRunning" 的 getter)。