ich eine UIView-Unterklasse habe, wo ich versuche, ein abgerundetes Rechteck mit einem Schlagschatten zu ziehen. Obwohl es beide Elemente zeichnet, kann ich Schatten durch die abgerundete Rechteckfüllung sehen. Ich bin neu in CG, also vermisse ich wahrscheinlich etwas Einfaches (obwohl es nicht das Alpha der Füllung zu sein scheint, das auf 1 gesetzt ist). Hier ist der Zeichenrect-Code.UIView mit abgerundetem Rechteck und Schlagschatten: Schatten erscheint über Rechteck
- (void)drawRect:(CGRect)rect {
// get the contect
CGContextRef context = UIGraphicsGetCurrentContext();
//for the shadow, save the state then draw the shadow
CGContextSaveGState(context);
CGContextSetShadow(context, CGSizeMake(4,-5), 10);
//now draw the rounded rectangle
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
//since I need room in my rect for the shadow, make the rounded rectangle a little smaller than frame
CGRect rrect = CGRectMake(CGRectGetMinX(rect), CGRectGetMinY(rect), CGRectGetWidth(rect)-30, CGRectGetHeight(rect)-30);
CGFloat radius = self.cornerRadius;
// the rest is pretty much copied from Apples example
CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect);
CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect);
// Start at 1
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 & stroke the path
CGContextDrawPath(context, kCGPathFillStroke);
//for the shadow
CGContextRestoreGState(context);
}
dieses Blog-Post lesen http://bynomial.com/blog/?p=52&cpage=1#comment-1115. Es beantwortet wie man das macht. –
Die hier dokumentierte Lösung ist die beste für mich, weil sie mir zeigt, wie man ein benutzerdefiniertes Bild (mit maskToBounds = YES) sowie den Schatten verwendet. Vielen Dank! – Mark
@John: Vielen vielen Dank für den Link, Kumpel ... Hat mir geholfen, zu starten. :) – viral