我真的很难让颜色选择器工作。在我无法将任何信息委托给电话后,我尝试了以下内容:
UIViewController为UIViewController命名为VCColorPicker.swift
UIView连接到HSBColorPicker.swift。
以下是VCColorPicker.swift(UIViewController)的代码
import UIKit
class VCColorPicker :UIViewController {
@IBOutlet weak var LabelColor: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
print("VCColorPicker loaded")
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
目标是通过点击UIView获得一个颜色,该颜色写入标签“LabelColor”的BackgroundColor中,该颜色放在VCColorPicker上。
不幸的是,出现了两个问题:首先,窗口不会与我尝试填充LabelColor的最后一行一起构建。 (应用程序不构建)如果我将其取出,VCColorPicker的屏幕将不会加载,并给出以下错误代码。
2016-02-17 22:47:27.798 MagicACC [13202:2720420] Interface Builder文件中的未知类HSBColorPicker。 加载VCColorPicker 2016-02-17 22:47:27.806 MagicACC [13202:2720420] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* -CGColor没有为UIColor定义;需要先转换颜色空间。'
这里是我在网页上找到并试图适应的HSBColorPicker的代码。
import UIKit
internal protocol HSBColorPickerDelegate : NSObjectProtocol {
func HSBColorColorPickerTouched(sender:HSBColorPicker, color:UIColor, point:CGPoint, state:UIGestureRecognizerState)
}
@IBDesignable
class HSBColorPicker: UIView {
@IBInspectable var elementSize: CGFloat = 1.0 {
didSet {
setNeedsDisplay()
}
}
private func initialize() {
print("HSCColorPicker loaded - initialize start")
self.clipsToBounds = true
let touchGesture = UILongPressGestureRecognizer(target: self, action: "touchedColor:")
touchGesture.minimumPressDuration = 0
touchGesture.allowableMovement = CGFloat.max
self.addGestureRecognizer(touchGesture)
print("HSCColorPicker loaded - initialize stop")
}
override init(frame: CGRect) {
super.init(frame: frame)
initialize()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialize()
}
override func drawRect(rect: CGRect) {
print("HSCColorPicker loaded - drawRect start")
let context = UIGraphicsGetCurrentContext()
for var y = CGFloat(0.0); y < rect.height; y=y+elementSize {
let saturation = y < rect.height / 2.0 ? CGFloat(2 * y) / rect.height : CGFloat(1.0)
let brightness = y < rect.height / 2.0 ? CGFloat(1.0) : 2.0 * CGFloat(rect.height - y) / rect.height
for var x = CGFloat(0.0); x < rect.width; x=x+elementSize {
let hue = x / rect.width
let color = UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1.0)
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, CGRect(x:x, y:y, width:elementSize,height:elementSize))
print("HSCColorPicker loaded - drawRect stop")
}
}
}
func getColorAtPoint(point:CGPoint) -> UIColor {
print("HSCColorPicker loaded - getColorAtPoint start")
let roundedPoint = CGPoint(x:elementSize * CGFloat(Int(point.x / elementSize)),
y:elementSize * CGFloat(Int(point.y / elementSize)))
let saturation = roundedPoint.y < self.bounds.height / 2.0 ? CGFloat(2 * roundedPoint.y) / self.bounds.height : CGFloat(1.0)
let brightness = roundedPoint.y < self.bounds.height / 2.0 ? CGFloat(1.0): 2.0 * CGFloat(self.bounds.height - roundedPoint.y) / self.bounds.height
let hue = roundedPoint.x / self.bounds.width
print("HSCColorPicker loaded - getColorAtPoint stop")
return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1.0)
}
func touchedColor(gestureRecognizer: UILongPressGestureRecognizer){
let vPoint = gestureRecognizer.locationInView(self)
vColor = getColorAtPoint(vPoint)
VCColorPicker.LabelColor.backgroundcolor = vColor
}
}
如果有人能告诉我如何更改实际工作的代码,我将非常感激。先感谢您。也许你也可以告诉我是否有更好的方法来做到这一点。如果您回答,请告诉我如何更改代码的确切信息。我试着用几本不同的书来理解它,但我无法弄清楚我做错了什么。
René
答案 0 :(得分:0)
确定。经过一行一行,我终于得到了它。错误与代码没有任何关系,但实际上与我实现的工作流程有关。该程序试图匹配尚未给出任何值的变量。 :-)所以真的,如果有人提出相同的错误代码,只要看看你是否可能错过了同样的问题。 :-)