我正在尝试从 CAAnimation
获取 UIView
对象。我已经实现了以下 CAAnimationDelegate
方法
public func animationDidStop(_ animation:CAAnimation, finished:Bool) {
// Need respective View from "animation:CAAnimation"
}
该类(class)将执行具有不同 View 的多个动画。所以我需要在这个委托(delegate)方法中找出哪个View的动画完成了。如果有可能从这个动画中获得 View ,请指导我。
最佳答案
正如 matt 在此处建议的那样,您可以通过这种方式找到已完成的动画。
首先,您需要在创建动画时为动画添加不同的键值,如下所示:
let theAnimation = CABasicAnimation(keyPath: "opacity")
theAnimation.setValue("animation1", forKey: "id")
theAnimation.delegate = self
let theAnimation2 = CABasicAnimation(keyPath: "opacity")
theAnimation2.setValue("animation2", forKey: "id")
theAnimation2.delegate = self
在 animationDidStop
方法中你可以识别动画:
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
if let val = anim.value(forKey: "id") as? String {
switch val {
case "animation1":
print("animation1")
case "animation2":
print("animation2")
default:
break
}
}
}
我拍了THIS回答并将 Objective c 代码转换为带有 switch
大小写的 swift。
关于ios - 从 CAAnimation 获取 UIVIew,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55145653/