0
我希望View Controller中的步進器在每次單擊它時更新我自定義的UIView
中的顏色。我不確定問題是由類的實例引起的,還是變量的值(redd1
,greenn1
和bluee1
)未被更改。Swift UIView自定義視圖在ViewController動作後不會更新
這是我的第一個文件:
class ViewController: UIViewController {
var colors = UIView1();
@IBOutlet var redStepper: UIStepper!
@IBOutlet var greenStepper: UIStepper!
@IBOutlet var blueStepper: UIStepper!
@IBAction func redChange(sender: UIStepper)
{
redValue.text = Int(sender.value).description;
colors.redd1 = Double(sender.value);
//self.view.setNeedsDisplay()
}
@IBAction func greenChange(sender: UIStepper)
{
greenValue.text = Int(sender.value).description;
colors.greenn1 = Double(sender.value);
//self.view.setNeedsDisplay()
}
@IBAction func blueChange(sender: UIStepper)
{
blueValue.text = Int(sender.value).description;
colors.bluee1 = Double(sender.value);
//self.view.setNeedsDisplay()
}
}
這是我的第二個文件:
class UIView1: 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 circle = UIView(frame: CGRect(x: -75.0, y: -40.0, width: 200.0, height: 200.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);
}
}
遺憾的是它沒有工作,但欣賞它 – Steve
首先,嘗試添加在您的IBActions中打印(sender.value)以確保您收到正確的值。如果發生這種情況,請將print(redd1),print(greenn1)和print(bluee1)添加到updateColor()函數中,以查看是否接收值或運行。 –
不錯的主意。我試了一下,我所有的價值都被接收了。 – Steve