我正在尝试从CAAnimation获取UIView对象。我实现了以下CAAnimationDelegate方法
public func animationDidStop(_ animation:CAAnimation, finished:Bool) {
// Need respective View from "animation:CAAnimation"
}这个类将使用不同的视图执行多个动画。所以我需要找出哪个View的动画是在这个委托方法中完成的。请指导我,如果有任何可能性,以获得从这个动画的看法。
发布于 2019-03-13 23:47:38
正如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 case的swift。
https://stackoverflow.com/questions/55145653
复制相似问题