1

How is it possible in Swift to draw a line where each point can have a different color?

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. For my case, I have to make 600 Paths ( each dot a Path ) to get, what I would like to have.

Is there a simpler way?

Thank you very much!

EDIT: I've tried to create a 1x1 UIBezierPath and a CAShapeLayer for each. It works, but the whole app gets very very slow, so there must be a simpler or faster method, does anyone know a solution?

Manuel
  • 613
  • 1
  • 6
  • 20

2 Answers2

0

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. 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()
        }
    }
}
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Manuel
  • 613
  • 1
  • 6
  • 20
-1

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.

Charles Srstka
  • 16,665
  • 3
  • 34
  • 60
  • Thank you for the answer. So how would you draw the lines individually? As I mentioned if I use 1x1 point bezier paths, the app gets very very slow. – Manuel Aug 03 '17 at 04:18
  • Check out the first answer on this question: https://stackoverflow.com/questions/856354/how-do-i-draw-a-line-on-the-iphone – Charles Srstka Aug 03 '17 at 05:27
  • Hello, thank you, I've looked at the answer. I've tried this code ( converted to swift ) but I do need a custom UIView and can only use this with override drawRect, right? I need to change the lines and the colours every second ( at least ). – Manuel Aug 03 '17 at 19:14
  • Just call setNeedsDisplay() on the view when you need to redraw it, and your draw() method will be called again. – Charles Srstka Aug 03 '17 at 19:23
  • Okay I see, but I have an Array with 500 values, which holds the color data for each point in a line. And very second I need a new line with 500 new values for the color data. I can set e.g. the backgroundcolor for my new UIView from the ViewController, but how can I give the new UIView the big array of data, and tell him to update? Sorry for the maybe simple? question, but I haven't found anything useful with google – Manuel Aug 03 '17 at 19:34
  • Can you just give your UIView subclass a property which is an array of colors? Then in your didSet for the property you can have it call setNeedsDisplay() on itself. – Charles Srstka Aug 03 '17 at 21:13