2016-02-17 28 views
1

我真的很難讓顏色選擇器工作。之後,我無法得到它的任何信息委託回電話我嘗試了以下內容:試圖讓顏色選擇器在Swift 2.0中工作

命名爲VCColorPicker.swift連接到HSBColorPicker.swift一個UIViewController

的UIView

的UIViewController。

以下是代碼以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:27806 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

好的。一行一行完成後,我終於明白了。這個錯誤與代碼完全沒有任何關係,但實際上與我實施的工作流程有關。該程序試圖匹配尚未賦予任何價值的變量。 :-)真的,如果有人提出了相同的錯誤代碼,只是看看你是否可能錯過了同樣的問題。 :-)