UIView drawRect: Zeichnen Sie die invertierten Pixel, machen Sie ein Loch, ein Fenster, einen negativen Raum
Mit dem folgenden Code zeichne ich ein abgerundetes Rechteck. Es zeichnet ein schönes festes hellgrau gefülltes, abgerundetes Rechteck (in der Größe von "Selbst"). Eigentlich möchte ich die Pixelumkehrung zeichnen, das heißt, nicht ein festes, abgerundetes Rechteck, sondern ein Fenster oder Loch in der Form dieses runden Rechtecks in einem festen, hellgrauen Rechteck.
Gibt es eine umgekehrte Clip-Methode, die ich verwenden muss? Oder muss ich einen Bezierpfad verwenden? Entschuldigung, wenn dies sehr einfach ist, ich kann die Informationen jedoch nicht finden.
Danke fürs Lesen!
- (void)drawRect:(CGRect)rect
{
// get the context
CGContextRef context = UIGraphicsGetCurrentContext
CGContextSaveGState(context);
//draw the rounded rectangle
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextSetRGBFillColor(context, 0.8, 0.8, 0.8, 1.0);
CGContextSetLineWidth(context, _lineWidth);
CGRect rrect = CGRectMake(CGRectGetMinX(rect), CGRectGetMinY(rect), CGRectGetWidth(rect), CGRectGetHeight(rect));
CGFloat radius = _cornerRadius;
CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect);
CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect);
CGContextMoveToPoint(context, minx, midy);
// Add an arc through 2 to 3
CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
// Add an arc through 4 to 5
CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
// Add an arc through 6 to 7
CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
// Add an arc through 8 to 9
CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
// Close the path
CGContextClosePath(context);
// Fill the path
CGContextDrawPath(context, kCGPathFill);
CGContextRestoreGState(context);
}