我在其他人编写的视图控制器中使用颜色选择器(UIView子类),它包含几个处理触摸事件的子视图。当用户与子视图交互时,颜色选择器有一个公共变量" color"以及更新。
public class SwiftHSVColorPicker: UIView {
var colorWheel: ColorWheel!
...
public var color: UIColor!
...
}
请参阅:https://github.com/johankasperi/SwiftHSVColorPicker/blob/master/Source/SwiftHSVColorPicker.swift
我在视图控制器中引用了这个颜色选择器视图和标签:
@IBOutlet weak var colorPicker: SwiftHSVColorPicker!
@IBOutlet weak var output: UILabel!
如何更新标签文字" color"数据从颜色选择器视图变化?
答案 0 :(得分:1)
如果可能,您需要修改它 有两种方法:
1
var colorChanged : ((UIColor!)->())?// add a callback for the colorpicker
func hueAndSaturationSelected(hue: CGFloat, saturation: CGFloat) {
self.hue = hue
self.saturation = saturation
self.color = UIColor(hue: self.hue, saturation: self.saturation, brightness: self.brightness, alpha: 1.0)
brightnessView.setViewColor(self.color)
self.colorChanged?(self.color) //call it at this method
selectedColorView.setViewColor(self.color)
}
colorPicker.colorChanged = {[weak self] color in
self?.label.textColor = color //do something with your label
}
2
dynamic public var color: UIColor! // add the dynamic keyword
colorPicker.addObserver(self, forKeyPath: "color", options:[.Initial, .New], context: nil) // add an observer for the color
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
//do something with the color
if let color = change![NSKeyValueChangeNewKey] as? UIColor {
self.label.textColor = color
}
}