2017-10-05 82 views
-2

我有两种颜色的图像:在中心和白色圆环周围充满红色的圆圈。用两种不同颜色的色调图像

enter image description here

有着色两种不同颜色的图像的方法吗?例如:用绿色填充内圈,用蓝色填充外圈。

是否可以只为外圆改变颜色?

着色图像,如:

let image = UIImage(named: "circles")?.tintWithColor(UIColor.red) 

总是改变两个圆圈的颜色。

+0

你可以显示你在问题中描述的图像。 –

+2

您无法对图像进行更改,因此请使用UIView。 –

回答

1

你不能像这样对图像进行更改,所以使用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) 
    } 


} 

如何使用

  1. 从情节串连图板取一个UIView和分配类。

enter image description here

  • 现在使用属性

    • StrokeWidth:外圆宽度。

    • StrokeColor:外圈颜色。

    • outerFillColor:外圈填充颜色。

    • innerWidth:内圈宽度。

    • innerFillColor:内圈填充颜色。

    • 颜色: uiview的背景颜色,它已经在故事板中可用。这是额外的。

  • enter image description here

    输出

    enter image description here

    现在从故事板改改颜色和检查。属性立即应用于故事板,因此您不需要运行该项目。