我有一个通过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)
答案 0 :(得分:4)
TL / DR
创建一个表达式,在PaintCode中使用red
,green
,blue
,alpha
(external parameters
)并生成颜色(makeColor
在PaintCode中的函数)。然后,通过该表达式将生成的颜色分配给Stroke
,Fill
,无论如何。
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
}
演示
参考
答案 1 :(得分:1)