2

所以我有一个带有渐变子图层的背景视图,不断地动画以缓慢地改变颜色。我用 a 来做CATransaction,因为我还需要为其他属性设置动画:

CATransaction.begin()

gradientLayer.add(colorAnimation, forKey: "colors")
// other animations

CATransaction.setCompletionBlock({
    // start animation again, loop forever
}

CATransaction.commit()

现在我想复制这个渐变动画,比如说,一个按钮的标题。

期望的结果

注意 1:如果可能的话,我不能只在按钮上“打个洞”,因为我可能在按钮和背景之间有其他不透明的视图。

注意 2:按钮上的渐变位置并不重要。我不希望文本渐变复制下面的确切颜色,而是模仿背景的“情绪”。

因此,当创建按钮时,我将其渐变子层添加到注册层列表中,后台管理器也会更新:

func register(layer: CAGradientLayer) {
    let pointer = Unmanaged.passUnretained(layer).toOpaque()
    registeredLayers.addPointer(pointer)
}

因此,虽然在动画的下一次迭代中为文本渐变设置动画很容易,但我更希望按钮在添加后立即开始动画,因为动画通常需要几秒钟。如何复制背景动画,即将文本渐变设置为背景动画的当前状态,并使用右持续时间左和定时功能对其进行动画处理?

4

1 回答 1

2

beginTime正如@Shivam Gaur 的评论所建议的那样,解决方案确实是使用该属性。我实现它如下:

// The background layer, with the original animation
var backgroundLayer: CAGradientLayer!

// The animation
var colorAnimation: CABasicAnimation!

// Variable to store animation begin time
var animationBeginTime: CFTimeInterval!

// Registered layers replicating the animation
private var registeredLayers: NSPointerArray = NSPointerArray.weakObjects()

...

// Somewhere in our code, the setup function
func setup() {
    colorAnimation = CABasicAnimation(keyPath: "colors")
    // do the animation setup here
    ...
}
...

// Called by an external class when we add a view that should replicate the background animation
func register(layer: CAGradientLayer) {

    // Store a pointer to the layer in our array
    let pointer = Unmanaged.passUnretained(layer).toOpaque()
    registeredLayers.addPointer(pointer)

    layer.colors = colorAnimation.toValue as! [Any]?

    // HERE'S THE KEY: We compute time elapsed since the beginning of the animation, and start the animation at that time, using 'beginTime'
    let timeElapsed = CACurrentMediaTime() - animationBeginTime
    colorAnimation.beginTime = -timeElapsed

    layer.add(colorAnimation, forKey: "colors")
    colorAnimation.beginTime = 0
}

// The function called recursively for an endless animation
func animate() {

    // Destination layer
    let toLayer = newGradient() // some function to create a new color gradient
    toLayer.frame = UIScreen.main.bounds

    // Setup animation
    colorAnimation.fromValue = backgroundLayer.colors;
    colorAnimation.toValue = toLayer.colors;

    // Update background layer
    backgroundLayer.colors = toLayer.colors

    // Update registered layers (iterate is a custom function I declared as an extension of NSPointerArray)
    registeredLayers.iterate() { obj in
        guard let layer = obj as? CAGradientLayer else { return }
        layer.colors = toLayer.colors
    }

    CATransaction.begin()

    CATransaction.setCompletionBlock({
        animate()
    })

    // Add animation to background
    backgroundLayer.add(colorAnimation, forKey: "colors")

    // Store starting time
    animationBeginTime = CACurrentMediaTime();

    // Add animation to registered layers
    registeredLayers.iterate() { obj in
        guard let layer = obj as? CAGradientLayer else { return }
        layer.add(colorAnimation, forKey: "colors")
    }

    CATransaction.commit()

}
于 2019-12-06T17:55:03.617 回答