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?
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()
}
}
}
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.