Dies zieht keinen Schlaganfall:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor darkGrayColor].CGColor);
CGContextSetLineWidth(context, 14);
CGRect rrect = CGRectMake(CGRectGetMinX(rect), CGRectGetMinY(rect), CGRectGetWidth(rect), CGRectGetHeight(rect));
CGFloat radius = 30;
CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect);
CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect);
CGContextMoveToPoint(context, minx, midy);
CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawPath(context, kCGPathStroke);
}
EDIT: Dies ist, was (nach der richtigen Antwort) gearbeitet:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor darkGrayColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetLineWidth(context, 14);
CGRect pathRect = CGRectMake(10, 10, rect.size.width -20, rect.size.height -20);
CGPathRef path = [UIBezierPath bezierPathWithRoundedRect:pathRect cornerRadius:20].CGPath;
CGContextAddPath(context, path);
CGContextClip(context);
CGContextAddPath(context, path);
CGContextDrawPath(context, kCGPathEOFillStroke);
}
Dank! Könnten Sie sich meine "Antwort" oben ansehen und kommentieren? Diese Methode funktioniert nicht ganz. Der Strich zeichnet überhaupt nicht. Was ist Clip zu Pfad, der Methodenname? – Mrwolfy
Sie sind in der Nähe. [Die Dokumentation zu 'CGContextClip()'] (https://developer.apple.com/library/ios/documentation/graphicsimaging/reference/CGContext/Reference/reference.html#/apple_ref/c/func/CGContextClip) sagt : "Nach dem Festlegen des neuen Beschneidungspfads setzt die Funktion den aktuellen Pfad des Kontexts auf einen leeren Pfad zurück." Das erklärt, warum 'CGContextDraw' nichts zeichnet - Sie müssen den Pfad zum Kontext erneut hinzufügen, bevor Sie streichen. –
Verwenden von ['CGPath'] (https://developer.apple.com/library/ios/#documentation/graphicsimaging/Reference/CGPath/Reference/reference.html#//apple_ref/c/tdef/CGPathRef) oder [ 'UIBezierPath'] (http://developer.apple.com/library/ios/#documentation/uikit/reference/UIBezierPath_class/Reference/Reference.html) würde diesen Prozess vereinfachen - Sie könnten den Pfad einmal erstellen und dann verwenden es zweimal. –