Swift drawRect 函数不会更新绘图
Posted
技术标签:
【中文标题】Swift drawRect 函数不会更新绘图【英文标题】:Swift drawRect function will not update drawing 【发布时间】:2016-02-01 18:35:32 【问题描述】:我希望我的视图控制器中的步进器更新一个变量,该变量确定我的 UIView 中圆圈的颜色。我的变量正在更新,但我的 updateColor 函数中的圆圈没有被绘制出来。
视图控制器:
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);
【问题讨论】:
【参考方案1】:您需要在视图上调用 setNeedsDisplay() 方法。那将调用drawRect
所以 - 在 addSubview(circle) 之后添加方法
【讨论】:
还是不行。 updateColor 没有画圆圈,所以我认为这是问题以上是关于Swift drawRect 函数不会更新绘图的主要内容,如果未能解决你的问题,请参考以下文章
swift怎么在点击按钮时重新调用uiview的drawrect函数