In einer App zeichne ich einen gekrümmten UIBezierPath und eine MKOverlayPathView-Klasse, um Flugrouten anzuzeigen. Dies ist der Code ich verwende:Gefälle entlang eines gekrümmten UIBezierPfads zeichnen
- (UIBezierPath *)pathForOverlayForMapRect:(MKMapRect)mapRect { ... bla bla bla ... UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:s]; [path addQuadCurveToPoint:e controlPoint:cp1]; [path addLineToPoint:e2]; [path addQuadCurveToPoint:s2 controlPoint:cp2]; [path closePath]; return path; }
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context{
self.mapRect = mapRect;
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(context, mapRect.size.height/700);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextAddPath(context, [self pathForOverlayForMapRect:mapRect].CGPath);
[self updateTouchablePathForMapRect:mapRect];
CGContextDrawPath(context, kCGPathFillStroke);
}
Das funktioniert ganz gut, aber ich möchte einen Gradienten auf diesem Weg ziehen, anstatt nur eine Füllfarbe. Und hier beginnt es sehr schwierig zu werden.
Ich habe mit CGContextDrawLinearGradient() experimentiert, aber es hat mich noch nirgendwo nützlich nützlich.
Diese Antwort ist perfekt! Vielen Dank. Ich habe 'CGContextReplacePathWithStrokedPath()' entfernt, so dass mein Pfad mit dem Farbverlauf gefüllt und nicht nur davon umrissen würde. – freshking
'CGContextReplacePathWithStrokedPath' ist nützlich, wenn Sie einen Gradienten _along_ den Pfad (nach der Frage) und nicht _inside_ den Pfad wünschen. – neilco
Das Problem, das ich mit 'CGContextReplacePathWithStrokedPath' bekam, war, dass der Farbverlauf nur auf den Pfad selbst angewendet wurde, ihn aber nicht füllte. Strich statt Füllung. Oder habe ich etwas falsch gemacht? – freshking