iOS-Charts如何将UIImage放在一个点旁边

问题描述 投票:1回答:1

我想使用iOS-Charts和Swift绘制一些折线图来显示数据。现在,我要求在每个点旁边放一个小图标。

enter image description here

现在我可以在图形上添加UIImage,但问题是如何才能正确获得每个点的CGPoint坐标。

如果我改变设备或设备类型的方向,图像的位置仍然是固定的,不会相应改变。这是iPhone 6s图片。

enter image description here

这是我目前的代码:

for i in 0..<dataPoints.count {
        let pointLeft = lineChartView.getPosition(dataEntries[i], axis: .Left)

        let image = UIImage(named: numbers[i])
        let imageView = UIImageView(image:image!)
        imageView.frame = CGRect(x: pointLeft.x, y: pointLeft.y, width: 16, height: 16)
        lineChartView.addSubview(imageView)

    }
ios swift uiimage ios-charts
1个回答
3
投票

您可以修改private func drawCircles(context context: CGContext)中的LineChartRenderer.swift来执行以下操作:在圆圈绘制的循环内使用pt var,在那里计算并添加如下内容:

let image = UIImage(named: numbers[i])
image?.drawInRect(CGRect(x: pt.x, y: pt.y, width: 16, height: 16))

显然,您需要使用CGRect以您需要的方式放置图像。每次设备方向改变等时都会调用此方法。

© www.soinside.com 2019 - 2024. All rights reserved.