Uiview 卡莱尔无提示

Uiview 卡莱尔无提示,uiview,core-animation,calayer,cabasicanimation,Uiview,Core Animation,Calayer,Cabasicanimation,我正在尝试使用cabasicanitation对自定义对象(而不是UIView)的计时功能 我试图实现@CIFilter的答案,即使用CALayer的表示层,该层已设置动画,用于评估计时功能 我在viewdide中执行所有操作,因此存在一个有效的视图,但无论我做什么,Presentation层始终为零 请注意,我必须将动画添加到视图的层中,而不是添加到视图的层中,才能对其进行动画处理。如果我取消注释下面注释掉的行,我可以看到动画工作(但只有在设置根层动画时)。无论如何,表示层为零 我已经看了十几

我正在尝试使用
cabasicanitation
对自定义对象(而不是
UIView
)的计时功能

我试图实现@CIFilter的答案,即使用
CALayer
的表示层,该层已设置动画,用于评估计时功能

我在
viewdide
中执行所有操作,因此存在一个有效的视图,但无论我做什么,
Presentation
层始终为零

请注意,我必须将动画添加到视图的层中,而不是添加到视图的层中,才能对其进行动画处理。如果我取消注释下面注释掉的行,我可以看到动画工作(但只有在设置根层动画时)。无论如何,
表示
层为零

我已经看了十几个教程和答案,看起来这应该行得通,所以我想我一定是在做傻事

我只是尝试使用
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())
        }
    
    }
}

不确定您的代码是否能达到预期效果,但是

我认为
.presentation()
的原因是
nil
是因为您没有给
UIKit
一个应用动画的机会

试试这个:

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")

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

    if let presentationLayer = newView.layer.presentation() {
        let evaluatedValue = presentationLayer.bounds.origin.x
        print("immediate evaluatedValue: \(evaluatedValue)")
    }
    else {
        print("immediate", evaluatorLayer.presentation())
    }
    
}
我的调试输出是:

immediate nil
async evaluatedValue: 0.0

编辑

我仍然不确定你的目标是什么,但是试试看

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.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 = 1.0

    // set timeOffset on the animation, not on the layer itself
    basicAnimation.timeOffset = 0.3

    // add animation to evaluatorLayer
    evaluatorLayer.add(basicAnimation, forKey: "evaluate")
    
    DispatchQueue.main.async {
        
        // get presentation layer of evaluatorLayer
        if let presentationLayer = evaluatorLayer.presentation() {
            let evaluatedValue = presentationLayer.bounds.origin.x
            print("async evaluatedValue: \(evaluatedValue)")
        }
        else {
            print("async", evaluatorLayer.presentation())
        }
        
    }

}
在本例中,我们对动画而不是层应用
.timeOffset
。并且,我们将动画添加到
evaluatorLayer
,而不是
newView.layer

输出(用于我的快速测试):


我希望它能起作用,因为这就是我链接到的答案所说的,并且得到了支持。但是,是的,我现在得到一个表示层,但是
evaluatedValue
是0.0,即使我设置了时间偏移。所以有些地方仍然不对,但你确实回答了为什么演示文稿为零的问题,所以我接受了它。@JeshuaLacock-请参阅我答案的编辑。谢谢。这确实奏效了——一次。但每次都是0。嗯,我想用.async来实现我想用它实现的目的,所以我想我会用我现在拥有的。可耻 不公开CATimer函数以用于我们自己的对象。
async evaluatedValue: 30.000001192092896