Reputation: 1153
I have a stepper in my view controller that updates the variables (redd1,greenn1,bluee1). I have a circle that is drawn by over riding the drawRect function. The function updateColor() is suppose to draw a new circle with the updated color from the variables. When I print the new value of the variables they come up correct, but the color of the circle still doesn't change.
func updateColor()
{
let circle = UIView(frame: CGRect(x: -25.0, y: 10.0, width: 100.0, height: 100.0))
circle.layer.cornerRadius = 50.0;
let startingColor = UIColor(red: (CGFloat(redd1))/255, green: (CGFloat(greenn1))/255, blue: (CGFloat(bluee1))/255, alpha: 1.0)
circle.backgroundColor = startingColor;
addSubview(circle);
//print(redd1);
}
Upvotes: 0
Views: 53
Reputation: 59536
You are creating and adding a new circle (a UIView
) each time you want to change the color. Why don't you simply change the color of the existing circle?
class Foo: UIView {
var red: Float = 255
var green: Float = 0
var blue: Float = 0
private let circle = UIView(frame: CGRect(x: -25.0, y: 10.0, width: 100.0, height: 100.0))
private var circleColor: UIColor {
return UIColor(
red: (CGFloat(self.red))/255,
green: (CGFloat(green))/255,
blue: (CGFloat(blue))/255,
alpha: 1.0)
}
override init (frame: CGRect) {
super.init(frame: frame)
addCircle()
}
required init(coder aDecoder: NSCoder) {
fatalError("This class does not support NSCoding")
}
private func addCircle() {
circle.layer.cornerRadius = 50.0
circle.backgroundColor = self.circleColor
addSubview(circle)
}
func updateColor() {
circle.backgroundColor = self.circleColor
}
}
Upvotes: 1