Wie Polygone mit CGPath ziehen?
-
20-09-2019 - |
Frage
Ich habe durch die Dokumentation zu lesen aber es ist nicht sofort zu mir klar, wie ein Polygon zeichnen CGPath verwenden. Alles, was ich tun muß, ist CGPath um so etwas zu machen:
__
\ \
\ \
\__\
Könnte jemand bitte Schnipsel darüber, wie dies zu tun?
Außerdem gehe ich davon aus CGPathContainsPoint wird mir helfen, festzustellen, ob ein Punkt innerhalb solchen Weges ist ?, oder hat der Pfad hat eine feste Zeichnung sein
Auch, wie kann ich die CGPath bewegen? Ist das so einfach wie so etwas wie die Herkunft wie in CGRect zu ändern?
Danke.
-Oscar
Lösung
Sie sollten es tun, wie folgt:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
// Draw them with a 2.0 stroke width so they are a bit more visible.
CGContextSetLineWidth(context, 2.0);
for(int idx = 0; idx < self.points.count; idx++)
{
point = [self.points objectAtIndex:idx];//Edited
if(idx == 0)
{
// move to the first point
CGContextMoveToPoint(context, point.x, point.y);
}
else
{
CGContextAddLineToPoint(context, point.x, point.y);
}
}
CGContextStrokePath(context);
}
Hier ist zu beachten, ist die Punkte die Anordnung der Punkte, die Sie für das Polygon zeichnen möchten. So soll es Kreisbahn wie:. Sie sind ein Dreieck Punkte zeichnen (x1, x2, x3)
dann sollten Sie in einem Array (x1, x2, x3, x1)
geben
Hope, das hilft.
Andere Tipps
Dies ist ein Beispiel dafür, wie ein Dreieck mit CGPath zu erstellen, müssen Sie nur die Punkte setzen.
var path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, 0, 0) //start from here
CGPathAddLineToPoint(path, nil, 20, 44)
CGPathAddLineToPoint(path, nil, 40, 0)
CGPathAddLineToPoint(path, nil, 0, 0)
//and to use in SpriteKit, for example
var tri = SKShapeNode(path: path)
var color = NSColor.blueColor()
tri.strokeColor = color
tri.fillColor = color
Dies ist das Ergebnis
Siehe Apples QuartzDemo Anwendung. Es hat Code, dies zu tun, wie auch viele andere Quarz-Zeichenfunktionen.
Draelach des Antwort aktualisiert Swift 4:
let path = CGMutablePath()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: 20, y: 44))
path.addLine(to: CGPoint(x: 40, y: 0))
path.addLine(to: CGPoint(x: 0, y: 0))
let tri = SKShapeNode(path: path)
CS193P Klasse Stanford auf dem iPhone hatte ein Projekt HelloPoly genannt, das genau sein könnte, was Sie wollen - siehe Klasse Homepage für die Spezifikation und dann das Video sehen, wie es durchgeführt wurde (und google-Lösungen von Leuten, die die Zuordnung taten).