简体   繁体   English

快速更改自定义工程图的颜色4

[英]changing the color of a custom drawing in swift 4

So I've got a slider that changes the hue value of a UIColor. 因此,我有一个滑块可以更改UIColor的色相值。 I want my custom drawing to have exactly this color. 我希望我的自定义图形完全具有这种颜色。

My custom drawing class looks like this: 我的自定义绘图类如下所示:

class CircleView: UIView {

    var color = UIColor().white {
        didSet {
            setNeedsDisplay()
        }
    }

    override func draw(_ rect: CGRect) {

        if let context = UIGraphicsGetCurrentContext() {
            context.addArc(center: CGPoint(x: bounds.midX, y: bounds.midY), radius: bounds.height / 2, startAngle: 0, endAngle: 2*CGFloat.pi, clockwise: true)
            color.setFill()
            context.fillPath()
        }
    }

}

In my viewController, I have a property observer that should change the color of the circle, that my custom drawing class draws: 在我的viewController中,我有一个属性观察器,该观察器应更改自定义绘图类绘制的圆圈的颜色:

My properties: 我的属性:

private var hueValue: Float = 0.0 {
    didSet {
        color = UIColor(hue: CGFloat(hueValue), saturation: 1, brightness: 1, alpha: 1)
        backgroundView.backgroundColor = color
        circle.color = color
    }
}
var color = UIColor()
var circle = CircleView()

And my Action method: 而我的Action方法:

@IBAction func colorSliderChanged(_ sender: UISlider) {
        hueValue = sender.value
}

The Action method changes the hue value which changes the color of the backgroundView. Action方法会更改色相值,从而更改backgroundView的颜色。 But it does not change the color of the circle. 但这不会改变圆圈的颜色。 The circle stays white. 圆圈保持白色。

Any ideas? 有任何想法吗?

Here is a Screenshot of the app: 这是该应用程序的屏幕截图: 在此处输入图片说明

If you have a UIView added in your Storyboard, and you have set its Custom Class to CircleView , but you have not created an @IBOutlet connection to it, your code has no access to that view. 如果在情节提要中添加了UIView ,并且将其“自定义类”设置为CircleView ,但尚未 @IBOutlet创建@IBOutlet连接,则您的代码无权访问该视图。

If you have var circle = CircleView() but you have not added that view to your view, nothing you change to circle will show up on screen. 如果您具有var circle = CircleView()但尚未将该视图添加视图中, var circle = CircleView()改为circle任何内容都不会显示在屏幕上。

In your storyboard, add your UIView , change its class to CircleView , and then ctrl-drag to create your @IBOutlet . 在情节CircleView ,添加UIView ,将其类更改为CircleView然后按 ctrl拖动以创建@IBOutlet Xcode will automatically set it to @IBOutlet weak var circle: CircleView! Xcode会自动将其设置为@IBOutlet weak var circle: CircleView! , and you can set its color (don't forget to remove the var circle = CricleView() line). ,然后可以设置其颜色(不要忘记删除var circle = CricleView()线)。

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

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