現在我被困在這裏:
它的實際工作,儘快更新我的2-d陣列和我的「啓動」屬性,它正在重繪的UIView,但需要大約5秒鐘。有沒有人知道一個更快的方法來做到這一點?
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()
}
}
}
}
謝謝你的回答。那麼你將如何單獨繪製線條?正如我提到的,如果我使用1x1點bezier路徑,應用程序變得非常緩慢。 – Manuel
看看這個問題的第一個答案: https://stackoverflow.com/questions/856354/how-do-i-draw-a-line-on-the-iphone –
你好,謝謝,我已經看着答案。我試過這段代碼(轉換爲swift),但我確實需要一個自定義的UIView,並且只能用這個覆蓋drawRect,對吧?我需要每秒鐘更改線條和顏色(至少)。 – Manuel