2017-08-22 4 views
0

Ich möchte mehr als eine gerade Linie in Kerngrafiken zeichnen. oder speichern Sie die Zeile und starten Sie eine neue. Ich habe zwei Variablen für Berührungsposition und Berührungsstelle ihnen zugeordneten in Kontakt Begann, Verschoben, Ended dann habe ich dies:Zeichnen von Linien mit Kerngrafiken

override func draw(_ rect: CGRect) { 
    let context = UIGraphicsGetCurrentContext() 
    context?.setStrokeColor(UIColor(red: 0, green: 0, blue: 0, alpha: 1.0).cgColor) 
    context?.setLineWidth(5.0) 
    context?.move(to: CGPoint(x: firstTouchLocation.x, y: firstTouchLocation.y)) 
    context?.addLine(to: CGPoint(x: lastTouchLocation.x, y: lastTouchLocation.y)) 
    context?.strokePath() 
} 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if let touch = touches.first { 
     firstTouchLocation = touch.location(in: self) 
     lastTouchLocation = firstTouchLocation 
     setNeedsDisplay() 
    } 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if let touch = touches.first { 
     lastTouchLocation = touch.location(in: self) 
     setNeedsDisplay() 
    } 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if let touch = touches.first { 
     lastTouchLocation = touch.location(in: self) 
     setNeedsDisplay() 
    } 
} 
+2

Also, was ist das Problem? –

+0

die Zeile verschwindet und beginnt eine neue Ich möchte die Zeile speichern und starten Sie eine neue –

+0

@MohmedShowekh würden Sie den beschreibenden Problemkommentar zur Frage hinzufügen –

Antwort

0

Versuchen Sie, die Linienzeichnung CAShapeLayer mit etwa so:

func addLine(fromPoint start: CGPoint, toPoint end:CGPoint) { 
     let line = CAShapeLayer() 
     let linePath = UIBezierPath() 
     linePath.move(to: start) 
     linePath.addLine(to: end) 
     line.path = linePath.cgPath 
     line.strokeColor = UIColor.red.cgColor 
     line.lineWidth = 1 
     line.lineJoin = kCALineJoinRound 
     self.view.layer.addSublayer(line) 
    } 

Hope Diese hilft!

+0

würden Sie den Code für mehr klar erklären –

0

Sie müssen ein Modellobjekt haben, die eine Linie repräsentieren, sagen wir zB: LineAnnotation. LineAnnotation wird Start-und Endpunkte, Farbe und viele andere Details über die Linie halten. Halten Sie alle LineAnnotation (Linie) in einem Array. Iteriere durch dieses Array und zeichne eine Zeile nach der anderen. In Ihrem Fall zeichnen Sie mit den neuesten Daten. Zurück Punkte, die gezogen werden, werden E aktualisiert, wenn Sie setNeedsDisplay() aufrufen

0

Wie pro Äpfel Dokumentation (https://developer.apple.com/documentation/uikit/uiview/1622437-setneedsdisplay) setNeedsDisplay Methode zeichnet Ihrer Meinung nach, so dass Sie mit den neuesten Linie mit frischen Blick gelassen werden nur.

Um Ihr Problem zu lösen, ziehen Sie einfach Ihren Code in einer Methode und rufen Sie diese Methode auf, wenn Sie eine Berührung machten.

override func draw(_ rect: CGRect) { 
    drawLine() 
} 

func drawLine() 
{ 
let context = UIGraphicsGetCurrentContext() 
context?.setStrokeColor(UIColor(red: 0, green: 0, blue: 0, alpha: 1.0).cgColor) 
context?.setLineWidth(5.0) 
context?.move(to: CGPoint(x: firstTouchLocation.x, y: firstTouchLocation.y)) 
context?.addLine(to: CGPoint(x: lastTouchLocation.x, y: lastTouchLocation.y)) 
context?.strokePath() 
} 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
if let touch = touches.first { 
    firstTouchLocation = touch.location(in: self) 
    lastTouchLocation = firstTouchLocation 
    drawLine() 
} 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
if let touch = touches.first { 
    lastTouchLocation = touch.location(in: self) 
    drawLine() 
} 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
if let touch = touches.first { 
    lastTouchLocation = touch.location(in: self) 
    drawLine() 
} 

}