ios - 从 CAAnimation 获取 UIVIew

标签 ios swift uikit caanimation

我正在尝试从 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/

相关文章:

ios - 使用 Firebase 提供的 API key 无效

ios - UIlabel Sizetofit 异常行为

ios - 如何跳过登录 View Controller ?

iphone - 如何在 iPhone 模拟器中重置 NSUserDefaults 数据?

ios - 无限内存增长 - iOS

ios - 如何向 UITableView 添加部分?

ios - 带有 xcode 的分层主从应用程序

ios - 用颜色绘制 SKPhysicsBody 的形状

ios - 如何通过 UnsafePointer<Int32> 创建 Int32?

ios - iOS13/Xcode 11 上的大标题导航栏为黑色