swiftios9.2

trying to get Color Picker to work in Swift 2.0


I got a real hard time to get the color Picker to work. After i couldn't get it to delegate any information back to a call i tried the following:

UIViewController named VCColorPicker.swift for a UIViewController

UIView connected to HSBColorPicker.swift.

Following is the Code to 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.
}
}

The Goal is that i receive by clicking on the UIView a color that is written into the BackgroundColor of the Label "LabelColor" that is put on the VCColorPicker.

Unfortunately two Problems crop up: First, the window does not build with the last line where i try to fill LabelColor. (Application does not build) If i take it out the screen for VCColorPicker won't load with the following error code given.

2016-02-17 22:47:27.798 MagicACC[13202:2720420] Unknown class HSBColorPicker in Interface Builder file. VCColorPicker loaded 2016-02-17 22:47:27.806 MagicACC[13202:2720420] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -CGColor not defined for the UIColor ; need to first convert colorspace.'

Here the Code for the HSBColorPicker that i found here on the page and tried to adapt.

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

I would be really grateful if someone can tell me how i need to change the code that this is actually working. Thank you in advance. Maybe you could also tell me if there is a better method to do it. If you answer please give me exact info on how to alter the code. I have tried to understand it with a few different books but i can't figure what i'm doing wrong.

René


Solution

  • Ok. I finally got it after going through it all line by line. The error didn't have anything to do at all with the code but actually with the workflow that i had implemented. The program tried to to match a variable that had not been given any value yet. :-) So really, if someone comes up with the same error-code just have a look if you might have missed the same issue. :-)