Objective c 向圆形视图添加阴影

Objective c 向圆形视图添加阴影,objective-c,uiview,Objective C,Uiview,我试图在圆形视图后面添加一个阴影,但是我的尝试使我只在视图的边界上找到了一个阴影,并且这个阴影也不会在视图周围出现(仅在顶部)。这是我的密码: -(void)drawRect:(CGRect)dirtyRect{ CGContextRef ctx=UIGraphicsGetCurrentContext(); CGRect bounds=[self bounds]; // Figure out the centre of the bounds rectangle

我试图在圆形视图后面添加一个阴影,但是我的尝试使我只在视图的边界上找到了一个阴影,并且这个阴影也不会在视图周围出现(仅在顶部)。这是我的密码:

-(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);

}

谢谢你的阅读。

我想你是在逃避现实。尝试使剪切路径矩形变大一点,或使椭圆矩形变小一点


您可以通过关闭剪辑来测试这一点,并查看阴影是否出现。

好的,需要做一些事情来修复此问题。我认为这段代码可能会减少几行,但以下是有效的方法:

-(void)drawRect:(CGRect)dirtyRect{
    CGContextRef ctx=UIGraphicsGetCurrentContext();

    // Create bounds and path for rectangle slightly smaller than view (Thanks, Andrew T., for this!)
    CGRect bounds=[self bounds];
    CGFloat smallerBy=20.0;
    CGRect smallBounds=CGRectMake(bounds.origin.x+smallerBy/2, bounds.origin.y+smallerBy/2, bounds.size.width-smallerBy, bounds.size.height-smallerBy);
    CGPathRef path = CGPathCreateWithEllipseInRect(smallBounds, NULL);
    CGContextAddPath(ctx, path);

    // Add black outline with shadow to bounds
    [[UIColor blackColor] setStroke];
    CGContextSetLineWidth(ctx, 5.0);
    CGSize offset=CGSizeMake(3,4);
    CGColorRef colour=[[UIColor lightGrayColor] CGColor];
    CGContextSetShadowWithColor(ctx, offset, 8, colour);
    CGContextStrokePath(ctx);

    // Draw opaque white circle (to cover up shadow that was leaking "inside" image)
    [[UIColor whiteColor] setFill];
    CGContextSetLineWidth(ctx, 0.0);
    CGContextFillEllipseInRect(ctx, smallBounds);

    // Draw shadowless black outline over white circle (the circle had bitten into the original outline)
    CGContextSetShadowWithColor(ctx, offset, 3, NULL);
    CGContextAddPath(ctx, path);
    [[UIColor blackColor] setStroke];
    CGContextSetLineWidth(ctx, 5.0);
    CGContextStrokePath(ctx);

    // Clip context to bounds
    CGContextAddPath(ctx, path);
    CGContextClip(ctx);

    // Draw image
    UIImage *newImage=[UIImage imageNamed:@"image.png"];
    [newImage drawInRect:smallBounds];


}

谢谢你的回复。这可以解释为什么阴影不会四处移动,但是阴影出现在视图内部的问题(好像视图是透明的,并且显示阴影在后面)仍然存在。。。对这个有什么想法吗?我认为它运行正常,因为你没有填充椭圆。它正在显示线条的阴影。如果用纯色填充路径,将看不到它。