如何修复颜色不一致的径向渐变图层

时间:2018-12-28 12:58:41

标签: ios swift cagradientlayer

当尝试从白色绘制为清晰的径向渐变时,根据使用UIColor(white:alpha:)UIColor.white.withAlpha.Components(alpha:)还是UIColor.color的不同,可以获得不同的结果。如何获得清晰的颜色和纯色相同的渐变?

为了绘制径向渐变,我重写了draw(context :)方法(请参见下文)。当将纯色用于渐变时,我的代码似乎可以正常工作,但是当使用纯色时,我的代码“可神秘地”工作。

override func draw(in ctx: CGContext) {
    super.draw(in: ctx)

    ctx.saveGState()
    let gradient                = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colorFill as CFArray, locations: self.locations as! [CGFloat] )
    let startPoint              = CGPoint(x: startX, y: startY )
    let endPoint                = CGPoint(x: endX , y: endY)
    let drawingOption           : CGGradientDrawingOptions = radius > 0 ? [] : .drawsAfterEndLocation

    //self.masksToBounds          = true
    if gradientType == .radial {
        ctx.drawRadialGradient(gradient!, startCenter: startPoint, startRadius: 0.0, endCenter: endPoint, endRadius: self.frame.width / 2, options: drawingOption)
    } else {
        self.startPoint = startPoint
        self.endPoint   = endPoint
    }
}

这是我根据渐变的输入颜色获得的结果:

[UIColor.white.cgColor,UIColor.black.cgColor] (期望的结果是用清晰的颜色而不是黑色)

White to Black

[UIColor.white.cgColor, UIColor.clear.cgColor]

White to Clear

[UIColor.white.cgColor, UIColor.white.withAlphaComponent(0.0).cgColor]

White to White with alpha component

[UIColor(white: 0.0, alpha: 1).cgColor, UIColor(white: 1.0, alpha: 0).cgColor]

Gray layer with alpha component

有人可以解释为什么我得到的输出与仅纯色(我希望获得的输出)不同吗?

非常感谢您抽出宝贵的时间阅读和回复!

1 个答案:

答案 0 :(得分:0)

并非仅通过draw(in context:)方法绘制该层。此行代码确认在调用方法之前已设置了一些属性,该属性事先触发了(self.locations)层的绘制:

CGColorSpaceCreateDeviceRGB(), colors: colorFill as CFArray, locations: self.locations as! [CGFloat] )

通过在自定义初始化程序中删除self.colors和self.locations属性集,并将值存储在Type Properties中,而不是在调用self.setNeedDisplay()之前,仅在draw(in context:)方法中绘制图层

class CustomLayer {
    override init() {
        super.init()
        needsDisplayOnBoundsChange = true
    }
    
    override init(layer: Any) {
        super.init(layer: layer)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    var colors    = []()
    var colorMap  = [NSNumber]()
    
    custom init() {
      colors = [color,color]
      colorMap = [0.0, 1.0]
      self.setNeedDisplay()
    }
    
    override func draw(in ctx: CGContext) {
        super.draw(in: ctx)
        ctx.saveGState()
        guard let gradient          = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors as CFArray, locations: colorMap as! [CGFloat] ) else {return}
        let startPoint              = CGPoint(x: startX, y: startY )
        let endPoint                = CGPoint(x: endX , y: endY)
        self.masksToBounds          = true
        if gradientType == .radial {
            ctx.drawRadialGradient(gradient, startCenter: startPoint, startRadius: 0.0, endCenter: endPoint, endRadius: self.frame.width / 2, options: drawingOption)
        } else {
            self.locations          = colorMap
            self.colors             = colors
            self.startPoint         = startPoint
            self.endPoint           = endPoint
        }
    }
}

enter image description here

干杯!