Ios 为什么阴影半径也会对圆的内部产生影响?

Ios 为什么阴影半径也会对圆的内部产生影响?,ios,objective-c,calayer,shadow,Ios,Objective C,Calayer,Shadow,我想将阴影应用到圆的外层空间。但阴影半径也会对内部产生影响,因此核心圆看起来比实际更小 我画的圆圈是这样的: self.shadowLayer = [CALayer layer]; self.shadowLayer.frame = self.view.layer.bounds; self.shadowLayer.shadowColor = [UIColor blueColor].CGColor; self.shadowLayer.shadowRadius = 0; self.shadowLay

我想将阴影应用到圆的外层空间。但阴影半径也会对内部产生影响,因此核心圆看起来比实际更小

我画的圆圈是这样的:

self.shadowLayer = [CALayer layer];
self.shadowLayer.frame  = self.view.layer.bounds;
self.shadowLayer.shadowColor = [UIColor blueColor].CGColor;
self.shadowLayer.shadowRadius = 0;
self.shadowLayer.shadowOpacity = 1.0;
self.shadowLayer.shadowOffset = CGSizeMake(0,0);
CGRect frame = CGRectMake(0 , 50, self.view.bounds.size.width, self.view.bounds.size.width);
self.shadowLayer.shadowPath = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(frame, 40, 40)].CGPath;
[self.view.layer addSublayer:self.shadowLayer];
结果是法向圆:

但是如果我将self.shadowLayer.shadowRadius从0更改为30,结果是:

A您可以看到,核心中的纯色大小缩小了。我希望阴影仅在路径外部生效,并且纯色的大小与
阴影半径=0时的大小完全相同。在截图完成后,会添加红色曲线。只是为了看看大小的不同

更新
一个想法是将阴影半径减少一半,并以相同的数量扩展帧。我认为这将足够好。

您可以使用
NSShadow
并绘制自定义
UIView
来产生您想要的效果

使用以下代码绘制自定义视图:

- (void)drawRect: (CGRect)frame
{
    //// General Declarations
    CGContextRef context = UIGraphicsGetCurrentContext();

    //// Color Declarations
    UIColor* color4 = [UIColor colorWithRed: 0.301 green: 0.261 blue: 0.968 alpha: 1];
    UIColor* shadowColor = [UIColor colorWithRed: 0 green: 0 blue: 0 alpha: 1];

    //// Shadow Declarations
    NSShadow* shadow = [[NSShadow alloc] init];
    [shadow setShadowColor: shadowColor];
    [shadow setShadowOffset: CGSizeMake(0.1, -0.1)];
    [shadow setShadowBlurRadius: 11];

    //// Oval Drawing
    UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(CGRectGetMinX(frame) + 69, CGRectGetMinY(frame) + 14, 72, 70)];
    CGContextSaveGState(context);
    CGContextSetShadowWithColor(context, shadow.shadowOffset, shadow.shadowBlurRadius, [shadow.shadowColor CGColor]);
    [color4 setFill];
    [ovalPath fill];
    CGContextRestoreGState(context);
}
这将在圆边界外侧产生阴影效果:


尝试为阴影层提供-40,-40,因为CGRectInsett太大。在这两种情况下,圆圈的边框应该保持不变。如果使用的是
CGContextSetShadowWithColor
,则无需使用
NSShadow
。要使用后者,请调用该方法。如果要存储引用并将其作为参数发送,该怎么办?我们还可以用cgcontextsetshadowwithcolor这样做吗?