在ViewController操作之后,Swift UIView自定義視圖不會更新

[英]Swift UIView custom view won't update after ViewController action


I want the stepper in the View Controller to update the color in my custom UIView every time it is clicked. I'm not sure if the problem is due to the instance of the class or that the values of the variables (redd1, greenn1, and bluee1) are not being changed.

我希望View Controller中的步進器每次單擊時都會更新自定義UIView中的顏色。我不確定問題是由於類的實例還是由於變量(redd1,greenn1和bluee1)的值沒有被更改。

This is my first file:

這是我的第一個檔案:

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()
    } 
}

This is my second file:

這是我的第二個文件:

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

1 个解决方案

#1


0  

drawRect is only called when the view is being added, so in your example, drawRect is called once, and not after changing the stepper.

drawRect僅在添加視圖時調用,因此在您的示例中,drawRect被調用一次,而不是在更改步進器之后。

Instead, change your stepper file to:

而是將您的步進文件更改為:

@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);
    updateColor()
}

@IBAction func greenChange(sender: UIStepper) {
    greenValue.text = Int(sender.value).description;
    colors.greenn1 = Double(sender.value);
    updateColor()
}

@IBAction func blueChange(sender: UIStepper) {
    blueValue.text = Int(sender.value).description;
    colors.bluee1 = Double(sender.value);
    updateColor()
} 

And then add the updateColor function to your UIView1 class:

然后將updateColor函數添加到您的UIView1類:

class UIView1: UIView {

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

    func updateColor() {
        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);
    }
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/01/29/8b9f33611088afa03b2550f37a3c0910.html



 
  © 2014-2022 ITdaan.com 联系我们: