Iphone 带圆角矩形和阴影的UIView:阴影显示在矩形上方
我有一个uiview子类,我试图画一个带阴影的圆形矩形。虽然它同时绘制了两个元素,但我可以通过圆角矩形填充看到阴影。我是CG新手,所以我可能遗漏了一些简单的东西(虽然它似乎不是设置为1的填充的alpha)。这是DrawRect代码Iphone 带圆角矩形和阴影的UIView:阴影显示在矩形上方,iphone,objective-c,uiview,dropshadow,Iphone,Objective C,Uiview,Dropshadow,我有一个uiview子类,我试图画一个带阴影的圆形矩形。虽然它同时绘制了两个元素,但我可以通过圆角矩形填充看到阴影。我是CG新手,所以我可能遗漏了一些简单的东西(虽然它似乎不是设置为1的填充的alpha)。这是DrawRect代码 - (void)drawRect:(CGRect)rect { // get the contect CGContextRef context = UIGraphicsGetCurrentContext(); //for the shadow, save
- (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);
}
我认为你不可能一次就做到这一点。嗯,我改变了你的代码如下,这似乎是工作
- (void)drawRect:(CGRect)rect
{
// get the contect
CGContextRef context = UIGraphicsGetCurrentContext();
//now draw the rounded rectangle
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 0.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 = 45;
// 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);
{
//for the shadow, save the state then draw the shadow
CGContextSaveGState(context);
// 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);
CGContextSetShadow(context, CGSizeMake(4,-5), 10);
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
// Fill & stroke the path
CGContextDrawPath(context, kCGPathFillStroke);
//for the shadow
CGContextRestoreGState(context);
}
{
// 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);
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
// Fill & stroke the path
CGContextDrawPath(context, kCGPathFillStroke);
}
}
导入
QuartzCore/QuartzCore.h
yourView.layer.shadowColor = [[UIColor blackColor] CGColor];
yourView.layer.shadowOffset = CGSizeMake(10.0f, 10.0f);
yourView.layer.shadowOpacity = 1.0f;
yourView.layer.shadowRadius = 10.0f;
太好了,谢谢。很有意义:你画了一个矩形,画了一个阴影,它在圆角矩形的上方,所以你最后再画一次矩形。关于代码的一个注释。我在init语句中已将视图的背景设置为清晰。上面的代码适用于任何其他背景,但对于清晰的背景,我必须在第一个矩形被笔划和填充之前设置填充颜色。我了解到,您可以扩展渲染层的区域,这样它就不会被图幅剪裁。用法:layer.bounds=CGRectMake(-1,-1102102)。。。如果您的视图为100x100,阴影为像素偏移等,您就明白了。使用maskToBounds和setCornerRadius对圆角不起作用。请尝试添加视图。layer.shouldRasterize=YES;为了帮助提高性能…或者,将layer.shadowPath设置为shouldRasterize的替代方案。如何准确设置阴影路径?我不确定我到底应该在这里放什么..这里的一个警告是确保您的视图的背景设置为[UIColor clearColor],否则您将在视图的框架周围绘制阴影,忽略圆角。在drawRect:中设置图层属性是无用且多余的,因为除非你改变它们,否则它们不会改变。取消这些调用(例如,在init…)阅读此博客文章。这里介绍的解决方案最适合我,因为它向我展示了如何使用自定义图像(maskToBounds=YES)以及阴影。谢谢@约翰:非常感谢你的链接,伙计。。。帮助我开始。:)
- (void)drawRect:(CGRect)rect
{
self.layer.masksToBounds = NO;
self.layer.shadowColor = [[UIColor whiteColor] CGColor];
self.layer.shadowOffset = CGSizeMake(0,2);
self.layer.shadowRadius = 2;
self.layer.shadowOpacity = 0.2;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(20, 20)];
[[UIColor blackColor] setFill];
[path fill];
}