Updating context fill color on didSet

时间:2017-08-05 11:48:54

标签: ios swift uiview

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 个答案:

答案 0 :(得分:1)

Try the following one

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