gpt4 book ai didi

swift - UIView 实时复制另一个 View 的 CAAnimation?

转载 作者:行者123 更新时间:2023-12-04 10:57:50 24 4
gpt4 key购买 nike

所以我有一个带有渐变子层的背景 View ,连续动画以缓慢改变颜色。我正在用 CATransaction 做这件事,因为我还需要为其他属性设置动画:

CATransaction.begin()

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

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

CATransaction.commit()

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

Desired result

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

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

因此,当创建按钮时,我将其渐变子图层添加到已注册图层列表中,后台管理器也会更新:
func register(layer: CAGradientLayer) {
let pointer = Unmanaged.passUnretained(layer).toOpaque()
registeredLayers.addPointer(pointer)
}

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

最佳答案

解决方案确实是使用 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()

}

关于swift - UIView 实时复制另一个 View 的 CAAnimation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59068086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com