TL/DR
创建一个接受red
, green
, blue
, alpha
(external parameters
在 PaintCode 中)并生成颜色(makeColor
在 PaintCode 中的函数)的表达式。然后通过该表达式将生成的颜色分配给Stroke
,等。Fill
油漆代码
自定义视图
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
}
演示
参考
该项目可以从我的 GitHub 存储库中克隆。
另请查看PaintCode 表达式语言指南。