gpt4 book ai didi

Swift 函数不更新绘图

转载 作者:行者123 更新时间:2023-11-30 10:07:48 24 4
gpt4 key购买 nike

我的 View Controller 中有一个步进器,可以更新变量(redd1,greenn1,bluee1)。我有一个通过重写 drawRect 函数绘制的圆。函数 updateColor() 假设用变量中更新的颜色绘制一个新的圆。当我打印变量的新值时,它们显示正确,但圆圈的颜色仍然没有改变。

 func updateColor()
{
let circle = UIView(frame: CGRect(x: -25.0, y: 10.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);
//print(redd1);
}

最佳答案

每次您想要更改颜色时,您都会创建并添加一个新圆圈(UIView)。为什么不简单地改变现有圆圈的颜色呢?

class Foo: UIView {

var red: Float = 255
var green: Float = 0
var blue: Float = 0

private let circle = UIView(frame: CGRect(x: -25.0, y: 10.0, width: 100.0, height: 100.0))

private var circleColor: UIColor {
return UIColor(
red: (CGFloat(self.red))/255,
green: (CGFloat(green))/255,
blue: (CGFloat(blue))/255,
alpha: 1.0)
}

override init (frame: CGRect) {
super.init(frame: frame)
addCircle()
}

required init(coder aDecoder: NSCoder) {
fatalError("This class does not support NSCoding")
}

private func addCircle() {
circle.layer.cornerRadius = 50.0
circle.backgroundColor = self.circleColor
addSubview(circle)
}

func updateColor() {
circle.backgroundColor = self.circleColor
}
}

enter image description here

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

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