【问题标题】:CALayer sublayer shows up but context drawing within it not workingCALayer 子层显示,但其中的上下文绘图不起作用
【发布时间】:2017-04-30 02:51:31
【问题描述】:

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

我添加的第二个子层以正确的尺寸正确添加到前面,但是我在自定义 CALayer 类的draw(in 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){

    }
}

【问题讨论】:

  • 问得非常好。下次可能会尝试格式化好一点。
  • 我很抱歉,它的格式肯定不是很好,不幸的是我不得不在飞机上发布这个并且服务员刚刚要求我们收起我们的笔记本电脑,我试图在着陆前得到一个粗略的草稿!我会在几个小时内回家后修改它。向社区道歉!
  • @matt 另外,感谢您的回答!几个小时后我旅行回家后,我会尝试一下,并将我的问题重新格式化为一种不那么粗糙的格式!
  • 别担心,我已经为你格式化了。但是很高兴您提供了重现问题所需的完整代码。我希望每个人都这样做。

标签: ios swift quartz-graphics quartz-core


【解决方案1】:

问题出在这一行:

let CloseContext = UIGraphicsGetCurrentContext()

没有当前上下文。你的工作是绘制到ctx,即你收到的上下文。

长话短说,把那一行改成:

let CloseContext : CGContext? = ctx

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

【讨论】:

  • 你不会评论在drawRect:中添加层?
  • @robmayoff 这不是问题,所以没有。但请做我的客人!
  • 我非常感谢马特。实际使用参数中给出的上下文确实更有意义!我还发现你的资源非常有用(因为有人还在摸索 iOS 的原理),你是个好人!
猜你喜欢
  • 2012-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-22
  • 2015-11-15
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多