簡體   English   中英

在核心動畫開始時間之前分層樣式的正確方法是什么?

[英]What is a correct way to layer styling before begin time of core animation?

我使用 Core Animation 創建了一個函數,該函數將圖層高度從 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")
    }
}

並且圖層在動畫過程中確實轉換得很好,但它在開始時間之前和完成之后都會恢復到原始高度。 所以我不得不根據延遲時間設置isHidden

但我認為這不是一種安全的方式。 所以我想改進那個代碼。

在這種情況下你通常會怎么做?

嘗試設置animation.fillMode = .backwards 我認為這將使動畫將其fromValue應用於圖層,直到達到動畫的beginTime

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM