Steve
Steve

Reputation: 1153

Swift drawRect function will not update drawing

I want the stepper in my view controller to update a variable that determines the color of the circle in my UIView. My variables are getting updated, but the circle in my updateColor function is not getting drawn.

ViewController:

var colors = UIView1();

@IBOutlet var redValue: UILabel!
@IBOutlet var greenValue: UILabel!
@IBOutlet var blueValue: UILabel!

@IBOutlet var redStepper: UIStepper!
@IBOutlet var greenStepper: UIStepper!
@IBOutlet var blueStepper: UIStepper!

@IBOutlet var v: UIView1!

override func viewDidLoad()
{
    super.viewDidLoad()
}
override func didReceiveMemoryWarning()
{
    super.didReceiveMemoryWarning()
}
@IBAction func redChange(sender: UIStepper)
{
    redValue.text = Int(sender.value).description;
    colors.redd1 = (Double(sender.value))*30;
    colors.updateColor()
}
@IBAction func greenChange(sender: UIStepper)
{
    greenValue.text = Int(sender.value).description;
    colors.greenn1 = (Double(sender.value))*30;
    colors.updateColor()
}
@IBAction func blueChange(sender: UIStepper)
{
    blueValue.text = Int(sender.value).description;
    colors.bluee1 = (Double(sender.value))*30;
    colors.updateColor()
}

UIView:

var redd1 = 0.0;
var greenn1 = 0.0;
var bluee1 = 0.0;

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

override func drawRect(rect: CGRect)
{
    let circle2 = UIView(frame: CGRect(x: -25.0, y: 10.0, width: 100.0, height:100.0))
    circle2.layer.cornerRadius = 50.0
    let startingColor2 = UIColor(red: (CGFloat(redd1))/255, green: (CGFloat (greenn1))/255, blue: (CGFloat(bluee1))/255, alpha: 1.0)
    circle2.backgroundColor = startingColor2;
    addSubview(circle2);
    }

func updateColor()
{
    let circle = UIView(frame: CGRect(x: 0.0, y: 0.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);
}

Upvotes: 0

Views: 1422

Answers (1)

comp_sci5050
comp_sci5050

Reputation: 145

You need to call the method setNeedsDisplay() on your view. That will call drawRect

So - add the method after you addSubview(circle)

Upvotes: 0

Related Questions