gpt4 book ai didi

ios - 在 ios 中用不同的颜色重新动画渐变背景

转载 作者:搜寻专家 更新时间:2023-10-30 21:56:04 25 4
gpt4 key购买 nike

我想在动画时用不同的颜色重新动画渐变背景。我可以使用此代码成功地为渐变颜色设置动画。

let dayTopColor = CommonUtils.colorWithHexString("955EAC")
let dayBottomColor = CommonUtils.colorWithHexString("9F3050")
let dayToTopColor = CommonUtils.colorWithHexString("D15B52")
let dayToBottomColor = CommonUtils.colorWithHexString("CC4645")
let nightTopColor = CommonUtils.colorWithHexString("2D5E7C")
let nightBottomColor = CommonUtils.colorWithHexString("19337D")
let nightToTopColor = CommonUtils.colorWithHexString("21334E")
let nightToBottomColor = CommonUtils.colorWithHexString("101A55")
var isInSaudiArabia = false
var gradient : CAGradientLayer?
var toColors : AnyObject?
var fromColors : AnyObject?

func animateBackground(){
var layerToRemove: CAGradientLayer?
for layer in self.view.layer.sublayers!{
if layer.isKindOfClass(CAGradientLayer) {
layerToRemove = layer as? CAGradientLayer
}
}
layerToRemove?.removeFromSuperlayer()

self.gradient!.colors = [nightTopColor.CGColor, nightBottomColor.CGColor]
self.toColors = [nightToTopColor.CGColor, nightToBottomColor.CGColor]

self.view.layer.insertSublayer(self.gradient!, atIndex: 0)
animateLayer()
}

func animateLayer(){

self.fromColors = self.gradient!.colors!
self.gradient!.colors = self.toColors as? [AnyObject]
let animation : CABasicAnimation = CABasicAnimation(keyPath: "colors")
animation.delegate = self
animation.fromValue = fromColors
animation.toValue = toColors
animation.duration = 3.50
animation.removedOnCompletion = true
animation.fillMode = kCAFillModeForwards
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
animation.delegate = self

self.gradient!.addAnimation(animation, forKey:"animateGradient")
}

override func animationDidStop(anim: CAAnimation, finished flag: Bool) {

self.toColors = self.fromColors;
self.fromColors = self.gradient!.colors!

animateLayer()
}

CommonUtils.colorWithHexString() 是一个将十六进制颜色转换为 UIColor 的函数。顺便说一句,当我在制作动画时尝试将背景颜色更改为白天颜色时,背景渐变颜色闪烁。

有知道解决方法的吗

最佳答案

问题是,当您移除图层时,它会停止动画。但是当动画停止时,animationDidStop 仍然被调用,它本身开始一个新的动画。因此,您正在移除图层,这会停止动画,并立即启动另一个动画,但您随后又开始了另一个动画。你有决斗动画。

您可以检查 flag 以查看动画是否在 animationDidStop 调用 animateLayer 之前正确完成。

override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
if flag {
toColors = fromColors;
fromColors = gradient!.colors!

animateLayer()
}
}

就我个人而言,我不确定您为什么要删除、添加和删除图层。如果你是,我不确定你为什么不只是 gradient?.removeFromSuperlayer() 而不是迭代层。

无论如何,我只是将 gradient 层保留在那里,只需检查它的 presentationLayer 并从那里开始动画:

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

fromColors = [dayTopColor.CGColor, dayBottomColor.CGColor]
toColors = [dayToTopColor.CGColor, dayToBottomColor.CGColor]

gradient = CAGradientLayer()
gradient!.colors = fromColors!
gradient!.frame = view.bounds
view.layer.addSublayer(gradient!)

animateLayer()
}

let dayTopColor = CommonUtils.colorWithHexString("955EAC")
let dayBottomColor = CommonUtils.colorWithHexString("9F3050")
let dayToTopColor = CommonUtils.colorWithHexString("D15B52")
let dayToBottomColor = CommonUtils.colorWithHexString("CC4645")

let nightTopColor = CommonUtils.colorWithHexString("2D5E7C")
let nightBottomColor = CommonUtils.colorWithHexString("19337D")
let nightToTopColor = CommonUtils.colorWithHexString("21334E")
let nightToBottomColor = CommonUtils.colorWithHexString("101A55")

var gradient : CAGradientLayer?
var toColors : [CGColor]?
var fromColors : [CGColor]?

var day = true

func toggleFromDayToNight() {
day = !day

if day {
fromColors = [dayTopColor.CGColor, dayBottomColor.CGColor]
toColors = [dayToTopColor.CGColor, dayToBottomColor.CGColor]
} else {
fromColors = [nightTopColor.CGColor, nightBottomColor.CGColor]
toColors = [nightToTopColor.CGColor, nightToBottomColor.CGColor]
}

let colors = (gradient!.presentationLayer() as! CAGradientLayer).colors // save the in-flight current colors
gradient!.removeAnimationForKey("animateGradient") // cancel the animation
gradient!.colors = colors // restore the colors to in-flight values
animateLayer() // start animation
}

func animateLayer() {
let animation : CABasicAnimation = CABasicAnimation(keyPath: "colors")
animation.fromValue = gradient!.colors
animation.toValue = toColors
animation.duration = 3.50
animation.removedOnCompletion = true
animation.fillMode = kCAFillModeForwards
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
animation.delegate = self

gradient!.colors = toColors

gradient!.addAnimation(animation, forKey:"animateGradient")
}

override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
if flag {
swap(&toColors, &fromColors)
animateLayer()
}
}

@IBAction func didTapButton() {
toggleFromDayToNight()
}

}

关于ios - 在 ios 中用不同的颜色重新动画渐变背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37471096/

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