CALayer 演示文稿 nil

问题描述

我正在尝试将 CABasicAnimation 用于自定义对象(不是 UIView)的计时功能。

我正在尝试实现来自 here 的 @CIFilter 的答案,即使用 CALayer 的动画表示层来评估计时功能。

我在 viewDidAppear 中完成所有操作,因此存在有效视图,但无论我做什么,Presentation 层始终为零。

请注意,我必须将动画添加到视图的图层,而不是我添加到它的图层,以便它完全设置动画。如果我取消注释下面注释的行,我可以看到动画有效(但仅在为根层设置动画时)。无论如何,Presentation 层为零。

我查看了数十篇教程和 SO 答案,看来这应该可行,所以我想我一定是在做一些愚蠢的事情。

我只是想使用 CoreAnimation 计时功能。我有 UICubicTimingParameters 工作,但似乎走 CA 路线提供了更多功能,这会很好。

import UIKit

class ViewController: UIViewController {

    override func viewDidAppear(_ animated: Bool) {
        
        super.viewDidAppear(animated)
    
        let newView = UIView(frame: view.frame)
        view.addSubview(newView)

        let evaluatorLayer = CALayer()
        evaluatorLayer.frame = CGRect(x: 0.0,y: 0.0,width: 100.0,height: 100.0)
        evaluatorLayer.borderWidth = 8.0
        evaluatorLayer.borderColor = UIColor.purple.cgColor
        evaluatorLayer.timeOffset = 0.3
        evaluatorLayer.isHidden = true
    //  evaluatorLayer.isHidden = false
    
        newView.layer.addSublayer(evaluatorLayer)
    
        let basicAnimation = CABasicAnimation(keyPath: "bounds.origin.x")
        basicAnimation.duration = 1.0
        basicAnimation.fromValue = 0.0
        basicAnimation.toValue = 100.0
        basicAnimation.fillMode = .forwards
        basicAnimation.isRemovedOnCompletion = false
        basicAnimation.speed = 0.0
    //  basicAnimation.speed = 0.1
    
        newView.layer.add(basicAnimation,forKey: "evaluate")

        if let presentationLayer = newView.layer.presentation() {
            let evaluatedValue = presentationLayer.bounds.origin.x
            print("evaluatedValue: \(evaluatedValue)")
        }
        else {
            print(evaluatorLayer.presentation())
        }
    
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)