Ios 在开始核心动画之前,正确的图层样式设置方法是什么?

Ios 在开始核心动画之前,正确的图层样式设置方法是什么?,ios,swift,core-animation,calayer,Ios,Swift,Core Animation,Calayer,我创建了一个带有核心动画的函数,可以将层高度从0设置为N,并且可以延迟 extension CALayer { func animate(to height: CGFloat, duration: Double, delay: Double) { let animation: CABasicAnimation = .init(keyPath: "bounds.size.height") animation.fromValue = 0 an

我创建了一个带有核心动画的函数,可以将层高度从0设置为N,并且可以延迟

extension CALayer {
    func animate(to height: CGFloat, duration: Double, delay: Double) {
        let animation: CABasicAnimation = .init(keyPath: "bounds.size.height")

        animation.fromValue = 0
        animation.toValue = height
        animation.beginTime = CACurrentMediaTime() + delay
        animation.duration: duration
        animation.timingFunction = .init(name: kCAMediaTimingFunctionEaseInEaseOut)

        // I want to improvement this part.
        //
        // self.isHidden = true
        //
        // DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
        //     self.isHidden = false
        // }

        self.bounds.size.height = height
        self.add(animation, forKey: "bounds.size.height")
    }
}
在动画过程中,层确实可以很好地变换,但在开始时间之前和结束之后,层会返回到原始高度。所以我必须根据延迟时间设置图层的
ishiden

但我认为这不是一个安全的方法。所以我想改进代码


在这种情况下,您通常做什么?

尝试设置
animation.fillMode=.backwards
。我认为这将使动画将其
fromValue
应用于层,直到达到动画的
beginTime

谢谢!此外,我还必须编写“animation.fillMode=kCAFillModeBackwards”。)