2016-02-01 119 views
0

我想讓我的視圖控制器中的步進器更新一個變量,它決定了我UIView中圓的顏色。我的變量正在更新,但我的updateColor函數中的圓圈沒有繪製。Swift drawRect函數不會更新圖形

的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); 
} 

回答

0

你需要調用方法setNeedsDisplay()對你的看法。這將調用drawRect

所以 - addSubview之後添加方法(圓圈)

+0

它仍然沒有工作。 updateColor不繪製圓,所以我認爲這是問題 – Steve