Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios CALayer子层显示,但其中的上下文绘制不起作用_Ios_Swift_Quartz Graphics_Quartz Core - Fatal编程技术网

Ios CALayer子层显示,但其中的上下文绘制不起作用

Ios CALayer子层显示,但其中的上下文绘制不起作用,ios,swift,quartz-graphics,quartz-core,Ios,Swift,Quartz Graphics,Quartz Core,我创建了一个自定义UIView类,在该类中我重写了draw(rect:CGRect)函数。在其中,我添加了一个gradient(CAGradientLayer)子层(工作起来没有太多问题),然后在上面添加了另一个子层,一个从CALayer继承的自定义“CloseLayer”类 我添加的第二个子层以正确的尺寸正确地添加到前面,但是我在自定义CALayer类的draw(在ctx:CGContext中)函数中实现的线条绘制似乎没有任何作用。在过去的几天里,我一直在浏览一些资源和谷歌搜索,我开始有点泄气

我创建了一个自定义UIView类,在该类中我重写了
draw(rect:CGRect)
函数。在其中,我添加了一个gradient(CAGradientLayer)子层(工作起来没有太多问题),然后在上面添加了另一个子层,一个从CALayer继承的自定义“CloseLayer”类

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

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

问题在于这一行:

let CloseContext = UIGraphicsGetCurrentContext()
没有当前上下文。你的工作是将你所接触到的内容融入到ctx中

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

let CloseContext : CGContext? = ctx

…还有宾果,你会看到你的台词的。(但这只是为了即时满足。最后,你应该摆脱
CloseContext
无处不在,取代
ctx

看看我的书:你不会在
drawRect:
?@robmayoff中对添加层发表评论这不是问题,所以不。但请做我的客人!我真是太感谢你了,马特。作为一个实际使用参数中给出的上下文的用户,这确实更有意义!我还发现你的资源非常有用(因为有人还在研究iOS的诀窍),你是个好人!看看我的书:你不会在
drawRect:
?@robmayoff中对添加层发表评论这不是问题,所以不。但请做我的客人!我真是太感谢你了,马特。作为一个实际使用参数中给出的上下文的用户,这确实更有意义!我还发现你的资源非常有用(因为有人还在研究iOS的诀窍),你是个好人!问得很好。下次可能会尝试更好的格式。我很抱歉,它的格式肯定不好。不幸的是,我不得不在飞机上发布这篇文章,乘务员刚刚让我们把笔记本收起来,我试图在着陆前输入一份粗略的草稿!几小时后我一到家就会修改它。向社区道歉@马特,我也很感激你的回答!我会尝试一下,并且在几个小时内旅行回家后,将我的问题重新格式化为一种不那么粗糙的格式!别担心,我给你格式化了。但是,您提供了重现问题所需的完整代码,这非常棒。我希望每个人都能这样做。这个问题问得很好。下次可能会尝试更好的格式。我很抱歉,它的格式肯定不好。不幸的是,我不得不在飞机上发布这篇文章,乘务员刚刚让我们把笔记本收起来,我试图在着陆前输入一份粗略的草稿!几小时后我一到家就会修改它。向社区道歉@马特,我也很感激你的回答!我会尝试一下,并且在几个小时内旅行回家后,将我的问题重新格式化为一种不那么粗糙的格式!别担心,我给你格式化了。但是,您提供了重现问题所需的完整代码,这非常棒。我希望每个人都这样做。