简体   繁体   English

Swift-如何在每个点可以具有单独颜色的位置上画一条线

[英]Swift - how to draw a line where each point can have an individual colour

How is it possible in Swift to draw a line where each point can have a different color? 在Swift中如何在每个点可以具有不同颜色的位置绘制一条线?

So let's say I have some data incoming which represents the color value of each dot in a line. 假设我收到了一些数据,它们代表一行中每个点的颜色值。 How can I accomplish that? 我该怎么做? If I use UIBezierPath, I have a Path and I can only give the path a color. 如果使用UIBezierPath,则有一个Path,并且只能为该路径赋予颜色。 For my case, I have to make 600 Paths ( each dot a Path ) to get, what I would like to have. 对于我的情况,我必须获得600条路径(每个点一个Path)才能得到我想要的东西。

Is there a simpler way? 有没有更简单的方法?

Thank you very much! 非常感谢你!

EDIT: I've tried to create a 1x1 UIBezierPath and a CAShapeLayer for each. 编辑:我试图为每个创建一个1x1 UIBezierPath和CAShapeLayer。 It works, but the whole app gets very very slow, so there must be a simpler or faster method, does anyone know a solution? 它可以工作,但是整个应用程序变得非常非常慢,因此必须有一个更简单或更快速的方法,有人知道解决方案吗?

Now I am stuck here: 现在我被困在这里:

it actually works, as soon as I update my 2-D Array and my "start" property, it is redrawing the UIView, but that needs about 5 seconds. 它实际上有效,一旦我更新了二维数组和“ start”属性,它就会重新绘制UIView,但这大约需要5秒钟。 Does anyone know a faster method for doing this? 有人知道这样做的更快方法吗?

class CustomView: UIView {

public var context = UIGraphicsGetCurrentContext()

public var redvalues = [[CGFloat]](repeating: [CGFloat](repeating: 1.0, count: 500), count: 500)

public var start = 0
{
    didSet{
        self.setNeedsDisplay()
    }
}

override func draw(_ rect: CGRect
{
    super.draw(rect)
    context = UIGraphicsGetCurrentContext()
    for yindex in 0...499{
        for xindex in 0...499 {
            context?.setStrokeColor(UIColor(red: redvalues[xindex][yindex], green: 0.0, blue: 0.0, alpha: 1.0).cgColor)

            context?.setLineWidth(2)
            context?.beginPath()
            context?.move(to: CGPoint(x: CGFloat(xindex), y: CGFloat(yindex)))
            context?.addLine(to: CGPoint(x: CGFloat(xindex)+1.0, y: CGFloat(yindex)))
            context?.strokePath()
        }
    }
}
}

I don't think there's any way to draw the different legs of a bezier path with different settings. 我认为没有任何方法可以使用不同的设置绘制贝塞尔曲线路径的不同分支。 Individually drawing the lines is probably the way to go. 单独画线可能是要走的路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM