2017-08-24 70 views
1

我有一個通過PaintCode繪製的圖標。我需要以編程方式在XCode中更改他的顏色。如何通過PaintCode&Xcode使用顏色變量

在PaintCode上,我將變量「ChevronColor」設置爲描邊顏色。

enter image description here

enter image description here

現在我有:

@IBDesignable 

class IconClass: UIView { 

    override func draw(_ rect: CGRect) { 
     StyleKit.drawIcon(frame: self.bounds, resizing: .aspectFit) 
    } 
} 

但我想補充的這種,能夠設置圖標的顏色。

@IBInspectable 
    var ChevronColor : UIColor { 
     didSet (newColor) { 
      setNeedsDisplay() 
    } 
} 

我不知道該怎麼做。

導出StyleKit文件後,我除外有在stylekit文件中可用此方法,但它並非如此:

StyleKit.drawIcon(frame: self.bounds, resizing: .aspectFit, chevronColor: self.ChevronColor) 

回答

4

TL/DR

創建一個表達式需要red,​​,bluealphaexternal parameters在PaintCode),並且產生的顏色(makeColor功能PaintCode)。然後所生成的顏色被分配給StrokeFill,經由表達什麼,。

PaintCode

PaintCode

自定義視圖

import Foundation 
import UIKit 

@IBDesignable class TreeView: UIView { 

    /* 
    * 
    * Notice - supported range for colors and alpha: 0 to 1. 
    * Color 0.808, 0.808, 0.808 = gray (starting color in this example). 
    * 
    */ 

    @IBInspectable var redColor: CGFloat = 0.808 { 
     didSet { 
      setNeedsDisplay() 
     } 
    } 

    @IBInspectable var greenColor: CGFloat = 0.808 { 
     didSet { 
      setNeedsDisplay() 
     } 
    } 

    @IBInspectable var blueColor: CGFloat = 0.808 { 
     didSet { 
      setNeedsDisplay() 
     } 
    } 

    @IBInspectable var alphaColor: CGFloat = 1 { 
     didSet { 
      setNeedsDisplay() 
     } 
    } 

    override func draw(_ rect: CGRect) { 
     StyleKit.drawTreeIcon(frame: rect, 
           resizing: .aspectFit, 
           red: redColor, 
           green: greenColor, 
           blue: blueColor, 
           alpha: alphaColor) 
    } 
} 

更改顏色例如

@IBAction func colorButtonPressed(_ sender: UIButton) { 
    // Get color references. 
    let red = CIColor(color: sender.backgroundColor!).red 
    let green = CIColor(color: sender.backgroundColor!).green 
    let blue = CIColor(color: sender.backgroundColor!).blue 
    let alpha = CIColor(color: sender.backgroundColor!).alpha 

    // Update the PaintCode generated icon. 
    treeView.redColor = red 
    treeView.greenColor = green 
    treeView.blueColor = blue 
    treeView.alpha = alpha 
} 

演示

Example

參考

該項目可以cloned from my GitHub repo.

也看看the PaintCode Expression Language guide.

+0

作品完美的感謝! – cmii