CALayer 子图层出现但其中的上下文绘图不起作用

CALayer sublayer shows up but context drawing within it not working

我创建了一个自定义 UIView class,我在其中覆盖了 draw(_ rect: CGRect) 函数。在其中,我添加了一个渐变 (CAGradientLayer) 子层(可以正常工作),然后在其上方添加另一个子层,一个继承自 CALayer 的自定义 'CloseLayer' class。

我添加的第二个子层以正确的尺寸正确地添加在前面,但是我在自定义 CALayer class 的 draw(in ctx: CGContext) 函数中实现的线条图似乎没有效果任何事物。在过去的几天里,我一直在查看一些资源并进行谷歌搜索,我开始有点泄气了。下面是我的 UIView class(包含两个子层)和自定义层 class 的代码,我希望在其中进行绘图。非常感谢任何帮助!

class GraphAView: UIView {
    // trading 9:00AM EST to 6:00PM EST
    // Normal: 9:30AM EST to 4:00PM EST
    // 9 hours total, 5 min intervals (12 intervals per hour, 108 total)
    let HonchoGreyComponents : [CGFloat] = [0.2,0.2,0.2,1.0]
    let graphBackGreyComponents: [CGFloat] = [0.8,0.8,0.8,1.0]


    override func draw(_ rect: CGRect) {
        setUpBack()
    }

    func setUpBack(){
        let RGBSpace = CGColorSpaceCreateDeviceRGB()
        let HonchoGrey = CGColor(colorSpace: RGBSpace, components: HonchoGreyComponents)
        let graphBackGrey = CGColor(colorSpace: RGBSpace, components: graphBackGreyComponents)
        let Gradlayer = CAGradientLayer()
        Gradlayer.frame = self.bounds
        Gradlayer.colors = [HonchoGrey!,graphBackGrey!,graphBackGrey!,HonchoGrey!]
        //layer.locations = [NSNumber(value: PreMarketEnd),NSNumber(value: RegMarketEnd)]
        Gradlayer.locations = [0,0.055555, 0.7777777]
        Gradlayer.startPoint = CGPoint(x: 0, y: 0)
        Gradlayer.endPoint =  CGPoint(x: 1, y: 0)
        self.layer.insertSublayer(Gradlayer, at: 0)
        let closingLay = CloseLayer()
        closingLay.frame = self.bounds
        let yer = UIColor(red: 0.3, green: 0.6, blue: 0.8, alpha: 1.0)
        //closingLay.backgroundColor = UIColor.blue.cgColor
        //closingLay.backgroundColor = UIColor(white: 0.4, alpha: 0.0).cgColor
        closingLay.backgroundColor = yer.cgColor
        self.layer.insertSublayer(closingLay, above: Gradlayer)
        closingLay.setNeedsDisplay()
        self.layer.setNeedsDisplay()
    }
}
class CloseLayer: CALayer {

    let ClosinglineWidth: CGFloat = 4.0
    let dashPattern: [CGFloat] = [2,2]

    override func draw(in ctx: CGContext) {

        let CloseContext = UIGraphicsGetCurrentContext()
        let middleY = self.frame.height / 2
       // let width = self.frame.width
       // let context = UIGraphicsGetCurrentContext()
        CloseContext?.setLineWidth(ClosinglineWidth)
        CloseContext?.setLineDash(phase: 1, lengths: dashPattern)
        CloseContext?.move(to: CGPoint(x: 0, y: middleY))
        CloseContext?.addLine(to: CGPoint(x: self.frame.width, y: middleY)) // 358 width
        let closingLineColor = UIColor.red.cgColor
        CloseContext?.setStrokeColor(closingLineColor)
        CloseContext?.strokePath()
    }

    func drawLine(start: CGPoint, end: CGPoint){

    }
}

问题出在这一行:

let CloseContext = UIGraphicsGetCurrentContext()

没有当前上下文。你的工作是融入ctx,你所得到的背景。

长话短说,将该行更改为:

let CloseContext : CGContext? = ctx

...还有宾果游戏,您会看到您的台词。 (但那只是为了一时的满足。最后,你应该把 CloseContext 处处去掉,代之以 ctx。)