Drawing a line with a shadow, but you only need to keep the shadow. IOS

I am trying to draw a line with a shadow, but I do not want to hold a line, but only a shadow.

I tried to set the color of the line stroke, but when I do this, the shadow also disappears.

The following code creates 2 lines, I only want to keep the shadow, because it looks more beautiful, and the pixel does not look like a line.

Is it possible?

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

Thanks.

+7
source share
2 answers

So, you cannot do this directly, because the shadow reflects the path, so if you make the path transparent, the shadow will also be transparent. There are some workarounds that I can come up with (depending on what you are doing), but one sneaky way is to just draw a shadow far enough below the path that you can just draw, basically by erasing it. For example, if the background is white, this will accomplish what you want:

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

hope this helps.

+9
source

why don't you just draw your line like a shadow. Do not create a shadow, but instead draw a black line with an alpha letter of 25. or less, and sweep it to where you expect the shadow to be.

+2
source

All Articles