UIView中的径向渐变颜色未更新

时间:2017-03-13 14:51:27

标签: ios swift swift3 radial-gradients

我试图在我的应用程序中使用背景UIView上的径向渐变。我的问题来了,我想多次更新渐变的视图颜色。我的代码没有错误,但我似乎无法弄清楚如何解决这个问题。

我尝试过的是在常规UIView中重新加载输入视图以及渐变类;删除uiview的子视图,并在屏幕上添加一个新视图,该视图仅用于更改设置颜色;我已经浏览了互联网,但似乎无法解决这个问题。我想要的只是UIView根据我给它的新颜色参数更新颜色。

这是我的径向渐变码:

import UIKit

class RadialGradient: UIView {


    var innerColor = UIColor.yellow
    var outterColor = UIColor.red

    override func draw(_ rect: CGRect) {

        let colors = [innerColor.cgColor, outterColor.cgColor] as CFArray
        let endRadius = min(frame.width, frame.height)

        let center = CGPoint(x: bounds.size.width/2, y: bounds.size.height/2)

        let gradient = CGGradient(colorsSpace: nil, colors: colors, locations: nil)

        UIGraphicsGetCurrentContext()!.drawRadialGradient(gradient!,
            startCenter: center,
            startRadius: 0.0,
            endCenter: center,
            endRadius: endRadius,
            options: CGGradientDrawingOptions.drawsAfterEndLocation)


    }

}

以下是我使用它的地方:

import UIKit

class TestIssuesVC: UIViewController {

    var check : Bool = false

    @IBAction func buttonClicked(_ sender: Any) {

        if check == true {
            backgroundsetting.removeFromSuperview()
            print("Why wont you change to purple and black?????")
            cheapFix(inner: UIColor.purple, outter: UIColor.black)

        } else {

        backgroundsetting.removeFromSuperview()

        cheapFix(inner: UIColor.red, outter: UIColor.blue)
            check = true
        }
    }

    func cheapFix(inner: UIColor, outter: UIColor) {

        let backgroundsetting = RadialGradient()

        backgroundsetting.innerColor = inner
        backgroundsetting.outterColor = outter

        backgroundsetting.frame = (frame: CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height * 0, width:self.view.frame.size.width, height: self.view.frame.size.height))
        self.view.addSubview(backgroundsetting)
        self.view.sendSubview(toBack: backgroundsetting)
        self.reloadInputViews()




    }
    let backgroundsetting = RadialGradient()


    override func viewWillAppear(_ animated: Bool) {
        backgroundsetting.innerColor = UIColor.green
        backgroundsetting.outterColor = UIColor.red

        backgroundsetting.frame = (frame: CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height * 0, width:self.view.frame.size.width, height: self.view.frame.size.height))
        self.view.addSubview(backgroundsetting)
        self.view.sendSubview(toBack: backgroundsetting)
        self.reloadInputViews()

    }


}

1 个答案:

答案 0 :(得分:0)

我看到两件事。

  1. 您的cheapFix方法永远不会更新backgroundsetting属性。它创建自己的同名局部变量。所以你实际上是一遍又一遍地添加新的视图,但每个都被发送到后面,所以你只能看到第一个。这就是为什么似乎没有任何改变。
  2. 这些都不是必需的。只需创建一个RadialGradient视图即可。当您想要改变颜色时,只需更新其颜色即可。需要修复该类,以便在更新其属性时重绘它。
  3. RadialGradient类中的两个属性进行以下更改:

    var innerColor = UIColor.yellow {
        didSet {
            setNeedsDisplay()
        }
    }
    var outterColor = UIColor.red {
        didSet {
            setNeedsDisplay()
        }
    }
    

    这些更改将确保视图在更新颜色时重绘。