用两种不同颜色的色调图像
问题描述:
我有两种颜色的图像:在中心和白色圆环周围充满红色的圆圈。用两种不同颜色的色调图像
有着色两种不同颜色的图像的方法吗?例如:用绿色填充内圈,用蓝色填充外圈。
是否可以只为外圆改变颜色?
着色图像,如:
let image = UIImage(named: "circles")?.tintWithColor(UIColor.red)
总是改变两个圆圈的颜色。
答
你不能像这样对图像进行更改,所以使用UIView。
使用此自定义类
import UIKit
@IBDesignable
class CircleView: UIView {
@IBInspectable var strokeWidth: CGFloat = 0
@IBInspectable var outerFillColor: UIColor = UIColor.clear
@IBInspectable var innerFillColor: UIColor = UIColor.red
@IBInspectable var strokeColor: UIColor = UIColor.clear
@IBInspectable var innerWidth: CGFloat = 0
@IBInspectable var bgColor: UIColor = UIColor.white {
didSet {
backgroundColor = bgColor
}
}
override func draw(_ rect: CGRect) {
self.backgroundColor = bgColor
let circlePath = UIBezierPath(ovalIn: rect)
let shapeLayer = CAShapeLayer()
shapeLayer.path = circlePath.cgPath
shapeLayer.fillColor = outerFillColor.cgColor
shapeLayer.strokeColor = strokeColor.cgColor
shapeLayer.lineWidth = strokeWidth
self.layer.addSublayer(shapeLayer)
let iFrame = CGRect(x: self.frame.width/2 - innerWidth/2,
y: self.frame.height/2 - innerWidth/2,
width: innerWidth, height: innerWidth)
let innerCirclePath = UIBezierPath(ovalIn: iFrame)
let shapeLayerInner = CAShapeLayer()
shapeLayerInner.path = innerCirclePath.cgPath
shapeLayerInner.fillColor = innerFillColor.cgColor
self.layer.addSublayer(shapeLayerInner)
}
}
如何使用
- 从情节串连图板取一个UIView和分配类。
你可以显示你在问题中描述的图像。 –
您无法对图像进行更改,因此请使用UIView。 –