我可以使用此代码成功为渐变颜色设置动画.
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 = falsevar 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,这将启动一个新动画本身.所以,你要删除停止动画的图层,立即启动另一个图层,但是你要开始另一个动画.你有决斗动画.您可以在animationDIDStop调用animateLayer之前检查标记以查看动画是否正确完成.
overrIDe func animationDIDStop(anim: CAAnimation,finished flag: Bool) { if flag { tocolors = fromcolors; fromcolors = gradIEnt!.colors! animateLayer() }}
就个人而言,我不确定你为什么要删除,添加和删除图层.如果你是,我不确定为什么你不只是渐变?.removeFromSuperlayer()而不是迭代层.
无论如何,我只是将渐变层保留在那里,只需检查其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,finished flag: Bool) { if flag { swap(&tocolors,&fromcolors) animateLayer() } } @IBAction func dIDTapbutton() { toggleFromDayToNight() }}总结
以上是内存溢出为你收集整理的重新动画渐变背景与不同的颜色迅速在ios全部内容,希望文章能够帮你解决重新动画渐变背景与不同的颜色迅速在ios所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)