Updating context fill color on didSet

Refresh

April 2019

Views

17 time

1

I made a custom UIView which is basically a colored circle. However, when I change the circle's color property, it's not updating. How can I do this?

Code

class CircleView : UIView {

    var color = UIColor.blue {
        didSet {
            // WHAT DO I DO!?
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func draw(_ rect: CGRect) {

        guard let context = UIGraphicsGetCurrentContext() else {return}

        context.addEllipse(in: rect)
        context.setFillColor(color.cgColor)
        context.fillPath()

    }
}

1 answers

1

Try the following one

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