Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Graphics - How to draw a shadow without drawing a line?

Take a look at this code:

- (void)drawRect:(CGRect)rect
{
  [super drawRect:rect];

  CGContextRef context = UIGraphicsGetCurrentContext();

  CGSize  myShadowOffset = CGSizeMake (-10,  15);

  CGContextSaveGState(context);

  CGContextSetShadow (context, myShadowOffset, 5);

  CGContextSetLineWidth(context, 4.0);
  CGContextSetStrokeColorWithColor(context,
                                 [UIColor blueColor].CGColor);
  CGRect rectangle = CGRectMake(60,170,200,200);
  CGContextAddEllipseInRect(context, rectangle);
  CGContextStrokePath(context);
  CGContextRestoreGState(context);
}

what it does it draws a circle and a shadow for this circle. However i can't make my mind how to draw only shadow without drawing circle's line? How do i do that? Answers to similar questions here on SO didn't help me

like image 657
Andrey Chernukha Avatar asked Dec 08 '22 08:12

Andrey Chernukha


2 Answers

I was able to achieve this using stroke color white, and blend mode multiply:

// ...
context.setStrokeColor(UIColor.white.cgColor)
context.setShadow(offset: CGSize.zero, blur: 8.0, color: UIColor.black.cgColor)
context.setBlendMode(.multiply)
context.addPath(...) <--- ADD YOUR PATH HERE
context.strokePath()
// ...

It draws only the shadow, without the stroke.

like image 118
lenooh Avatar answered Dec 11 '22 10:12

lenooh


Please try the following:

//If you would like your shadow color to be blue, change it to blue.
CGContextSetShadowWithColor(context, CGSizeMake(-10.0f,  15.0f), 5.0f, [UIColor lightGrayColor].CGColor);
CGContextSetStrokeColorWithColor(context, self.backgroundColor.CGColor);
CGContextStrokePath(context);

I just ran a test on it; and the effect seems desirable.

Hope this helps.

like image 39
Unheilig Avatar answered Dec 11 '22 09:12

Unheilig