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

How to use color variable with PaintCode & Xcode

我有一个通过 PaintCode 绘制的图标。我需要以编程方式在 XCode 中更改他的颜色。

在 PaintCode 上,我将变量 "ChevronColor" 设置为描边颜色。

现在我有:

@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)

TL/DR

创建一个表达式,接受 redgreenbluealpha(PaintCode 中的 external parameters)并生成颜色(makeColor PaintCode 中的函数)。然后通过该表达式将生成的颜色分配给 StrokeFill 等。

PaintCode

Custom View

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

Changing color example

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

Demo

Reference

项目可以cloned from my GitHub repo.

也看看the PaintCode Expression Language guide.

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