用阴影画一条线,但只想留下阴影.IOS

Rob*_*Rob 7 iphone core-graphics quartz-2d ios

我想用阴影绘制一条线,但我不想保留线但只留下阴影.

我试图将线条的笔触颜色设置为清除,但是当我这样做时,阴影也会消失.

下面的代码创建了2行,我只想保留阴影,因为它看起来更好,而且不像行那样像素化.

这可能吗?

    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);

    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
    CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);    

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat components[4] = {0.0, 0.0, 0.0, 1.0};
    CGColorRef shadowColor = CGColorCreate(colorSpace, components);
    CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(), CGSizeMake(10,10), 4.0, shadowColor);
Run Code Online (Sandbox Code Playgroud)

谢谢.

pho*_*ho0 9

因此,您无法直接执行此操作,因为阴影反映了路径,因此如果您使路径透明,阴影也将是透明的.我可以想到一些解决方法(取决于你正在做什么),但是一种偷偷摸摸的方式就是将阴影画到足够远的路径下方,你可以在它上面绘制,基本上擦除它.例如,如果背景为白色,则可以实现您想要的效果:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
    CGContextSetLineWidth(context, 10.0);
    CGContextMoveToPoint(context, 10.0, 30.0);
    CGContextAddLineToPoint(context, 310.0, 30.0);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat components[4] = {0.0, 0.0, 0.0, 1.0};
    CGColorRef shadowColor = CGColorCreate(colorSpace, components);
    CGContextSetShadowWithColor(context, CGSizeMake(0.0f,20.0f), 4.0, shadowColor);
    CGContextStrokePath(context);

    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
    CGContextSetLineWidth(context, 10.0);
    CGContextMoveToPoint(context, 10.0, 30.0);
    CGContextAddLineToPoint(context, 310.0, 30.0);
    CGContextStrokePath(context);
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.