1

来自 Apple 文档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)

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

4

1 回答 1

2

在 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
}

请注意,关键路径是因为Swift 中\.isRunning的属性 on被称为.UIViewPropertyAnimatorisRunning

这样做的好处是您不必知道给定属性的字符串是如何拼写的,并且更改后的值与观察到的属性具有相同的类型。


请注意,在 Objective-C 中,此 API 不可用,因此相应的 Objective-C 文档要求您观察“运行”属性。这是因为在 Objective-C 中,该属性称为“ running ”(但有一个称为“isRunning”的 getter)。

于 2018-04-24T09:00:56.580 回答