【问题标题】:Core Graphics Graph lines not drawing核心图形图形线不绘制
【发布时间】:2018-02-26 17:55:24
【问题描述】:

我正在使用this 代码尝试使用 Core Graphics 构建我自己的图形,除了水平线和 X 和 Y 轴没有绘制(在情节提要或模拟器中)之外,它一切正常,我不能在代码中看到任何会禁止他们绘图的内容?相关代码如下:

   override func draw(_ rect: CGRect) {
        super.draw(rect)

        context = UIGraphicsGetCurrentContext()

        // Graph size
        graphWidth = (rect.size.width - padding) - 10
        graphHeight = rect.size.height - 40
        axisWidth = rect.size.width - 10
        axisHeight = (rect.size.height - padding) - 10

        // Lets work out the highest value and round to the nearest 25.
        // This will be used to work out the position of each value
        // on the Y axis, it essentialy reperesents 100% of Y
        for (index, point) in data.enumerated() {
            let keys = Array(data[index].keys)
            let currKey = keys.first!
            if CGFloat(point[currKey]!) > everest {
                everest = CGFloat(Int(ceilf(Float(point[currKey]!) / 25) * 25))
            }
        }
        if everest == 0 {
            everest = 25
        }

        // Draw graph X-AXIS
        let xAxisPath = CGMutablePath()
        xAxisPath.move(to: CGPoint(x: padding, y: rect.size.height - 31))
        xAxisPath.move(to: CGPoint(x: axisWidth, y: rect.size.height - 31))
        context!.addPath(xAxisPath)

        context!.setStrokeColor(xAxisColor.cgColor)
        context!.strokePath()

        // Draw graph Y-AXIS
        let yAxisPath = CGMutablePath()
        yAxisPath.move(to: CGPoint(x: padding, y: 10))
        yAxisPath.move(to: CGPoint(x: padding, y: rect.size.height - 31))
        context!.addPath(yAxisPath)

        context!.setStrokeColor(yAxisColor.cgColor)
        context!.strokePath()

     let yLabelInterval : Int = Int(everest / 5)
            for i in 0...5 {

                let label = axisLabel(title: String(format: "%d", i * yLabelInterval))
                label.frame = CGRect(x: 0, y: floor((rect.size.height - padding) - CGFloat(i) * (axisHeight / 5) - 10), width: 30, height: 20) //I changed width from 20 to 30
                addSubview(label)


                if(showLines && i != 0) {
                    let line = CGMutablePath()
                    line.move(to: CGPoint(x: padding + 1, y: floor(rect.size.height - padding) - (CGFloat(i) * (axisHeight / 5))))
                    line.move(to: CGPoint(x: axisWidth, y: floor(rect.size.height - padding) - (CGFloat(i) * (axisHeight / 5))))
                    context!.addPath(line)
                    context!.setStrokeColor(linesColor.cgColor)
                    context!.strokePath()
                }
            }

【问题讨论】:

    标签: ios swift core-graphics


    【解决方案1】:

    我在 swift 4 中测试了您的代码。未显示轴线。另一种方法是,您可以使用如下所示的“addLines”函数。

    xAxisPath.addLines(between: [CGPoint(x: padding, y: rect.size.height - 31), CGPoint(x: axisWidth, y: rect.size.height - 31)])
    

    【讨论】:

    • 谢谢! ? 我很清楚我发布的代码(抚摸路径)是否正确绘制? IE。这是一个错误吗?不过,您的解决方案效果很好,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多