gpt4 book ai didi

ios - Swift drawRect 函数不会更新绘图

转载 作者:行者123 更新时间:2023-11-30 13:44:29 25 4
gpt4 key购买 nike

我希望 View Controller 中的步进器更新一个变量,该变量确定 UIView 中圆圈的颜色。我的变量正在更新,但 updateColor 函数中的圆圈没有被绘制。

View Controller :

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

UI View :

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

最佳答案

您需要在 View 上调用 setNeedsDisplay() 方法。这将调用drawRect

所以 - 在 addSubview(circle) 之后添加方法

关于ios - Swift drawRect 函数不会更新绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35137810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com