我试图在圆形 View 后面添加一个阴影,但我的尝试导致我只在 View 边框上添加了一个阴影——而且这个阴影也没有出现在 View 周围(只是顶部)。这是我的代码:
-(void)drawRect:(CGRect)dirtyRect{
CGContextRef ctx=UIGraphicsGetCurrentContext();
CGRect bounds=[self bounds];
// Figure out the centre of the bounds rectangle
CGPoint centre;
centre.x=bounds.origin.x+0.5*bounds.size.width;
centre.y=bounds.origin.y+0.5*bounds.size.height;
// Clip context
CGPathRef path = CGPathCreateWithEllipseInRect(bounds, NULL);
CGContextAddPath(ctx, path);
CGContextClip(ctx);
// Add black outline
path = CGPathCreateWithEllipseInRect(bounds, NULL);
CGContextAddPath(ctx, path);
[[UIColor blackColor] setStroke];
CGContextSetLineWidth(ctx, 3.0);
// Specify shadow
CGSize offset=CGSizeMake(1,4);
CGColorRef colour=[[UIColor darkGrayColor] CGColor];
CGContextSetShadowWithColor(ctx, offset, 2, colour);
// Draw image
UIImage *littleImage=[UIImage imageNamed:@"image.png"];
[littleImage drawInRect:bounds];
CGContextStrokePath(ctx);
}
感谢阅读。
最佳答案
我认为你正在剪掉阴影。尝试使剪切路径矩形大一点或椭圆矩形小一点。
您可以通过关闭剪裁来测试这一点,看看是否出现阴影。
关于objective-c - 向圆形 View 添加阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14271619/