2017-08-24 66 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