简体   繁体   English

如何切换CAGradientLayer?

[英]How to switch CAGradientLayer?

I initially create a CAGradientLayer on my first scene in viewDidLoad. 我最初在viewDidLoad的第一个场景上创建一个CAGradientLayer It's inserted like this: 它是这样插入的:

self.view.layer.insertSublayer(myCAGradientLayer, atIndex: 0)

That works fine. 很好 But now I need to change the gradient while the app is running. 但是现在我需要在应用运行时更改渐变。 The user will do something on scene 2 and come back to scene 1, where the gradient changes. 用户将在场景2上执行某些操作,然后返回到场景1,在此处渐变发生变化。 I build up a new CAGradientLayer and add to the view the same as above. 我建立了一个新的CAGradientLayer并添加到与上面相同的视图中。

But using viewWillAppear doesn't work. 但是,使用viewWillAppear无效。

Here is an example: 这是一个例子:

let gLocations: [Float] = [0.0, 1.0]
let gLayer: CAGradientLayer = CAGradientLayer()
gLayer.colors = [UIColor.yellowColor().CGColor, UIColor.greenColor().CGColor]
gLayer.frame = self.view.bounds
gLayer.locations = gLocations
self.view.layer.insertSublayer(gLayer, atIndex: 0)

Add the above to viewWillAppear. 将以上内容添加到viewWillAppear。 Then add it into a several conditionals using different colors. 然后将其添加到使用不同颜色的几个条件中。 Only the first conditional chosen appears. 仅出现第一个选择的条件。 Any of the other colors in the other conditionals doesn't appear. 其他条件中的任何其他颜色均不会出现。

I think part of the issue is that I'm always doing: 我认为部分问题是我一直在做:

self.view.layer.insertSublayer(gLayer, atIndex: 0)

Otherwise, the gradient will appear on top of other UI components. 否则,渐变将出现在其他UI组件的顶部。 I'm using several different gradients and they all use the above code. 我正在使用几种不同的渐变,它们都使用上面的代码。 But I'm guessing they all can't be inserted at index=0. 但是我猜它们都不能插入到index = 0处。

Is there some other way to change the gradient? 还有其他方法可以更改渐变吗?

The ViewWillAppear function should work. ViewWillAppear函数应该起作用。 It will be called every time the view is about to get active. 每次视图将被激活时都会调用它。

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated);
}

Update 更新
The first one is only displayed because your CAGradientLayer color is always set to be gLayer.colors = [UIColor.yellowColor().CGColor, UIColor.greenColor().CGColor] . 仅显示第一个,因为您的CAGradientLayer颜色始终设置为gLayer.colors = [UIColor.yellowColor().CGColor, UIColor.greenColor().CGColor]

Whenever you want to change the color you need to save this in the memory so that you know this when you enter the view again. 每当您要更改颜色时,都需要将其保存在内存中,以便再次进入视图时就知道这一点。 This is an alternative solution. 这是一个替代解决方案。

Create a NSUserDefault to save which color you want to show. 创建一个NSUserDefault以保存要显示的颜色。

// Set - set a new color whenever you want this to be displayed when you show the view next time
// forKey is the unique value to access the object you have saved
NSUserDefaults.standardUserDefaults().setObject("red", forKey: "color"

When you show the view do this: 显示视图时,请执行以下操作:

// Get
let color = NSUserDefaults.standardUserDefaults().stringForKey("color")

if color == "red"{
   gLayer.colors = [UIColor.redColor().CGColor, UIColor.blueColor().CGColor]
}
else if...

You can search your layer from sublayers: 您可以从子图层中搜索图层:

myCAGradientLayer.name = "my gradient layer"
        ...

extension UIView {
    func getMyGradient(name:String) -> CAGradientLayer {
        if let layers = self.layer.sublayers {
            for layer in layers {
                if (layer.name == name) {
                    let caGradientLayer = layer as! CAGradientLayer
                    return caGradientLayer
                }
            }
        }
        print("Sorry your gradient doesnt exist in this view..")
        return CAGradientLayer()
    }
}

To retrieve your gradient: 检索渐变:

let myCAGradientLayer = self.view.getMyGradient()
if myCAGradientLayer.name == "my gradient layer" {
   // do whatever you want with the same first gradient added 
}

Try to make setNeedsDisplay to your view, to re-render itself if there are problems around display update.. 尝试使setNeedsDisplay成为您的视图,如果在显示更新方面存在问题,则重新渲染自身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM