如何使用PaintCode&的颜色变量Xcode中

时间:2017-08-24 11:05:57

标签: swift instance-variables uicolor paint-code

我有一个通过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)

2 个答案:

答案 0 :(得分:4)

  

TL / DR

创建一个表达式,在PaintCode中使用redgreenbluealphaexternal parameters)并生成颜色(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.

答案 1 :(得分:1)

enter image description here

只需将“颜色”部分的访问级别中的颜色编辑为Parameter