Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I draw a curved shadow?

Like so:

enter image description here

I know that this will not work with NSShadow, drawing it in drawRect: will work just fine.

like image 682
IluTov Avatar asked Sep 07 '13 18:09

IluTov


People also ask

How do you shade a curved object?

By using a sharp coloured pencil and drawing parallel lines a curved surface can be produced. The techniques is quite simple - the closer the lines the darker they are drawn and the further apart lines the lighter they become.

How do you draw realistic shadows?

Apply Pencil Tone in Layers For Greater Realism It's far more effective to draw realistic shadows up in layers than to go in with a strong tone straight away. You will almost certainly get the values wrong in the early stages of your drawing and making the shading lighter is trickier than making it darker.


1 Answers

You can do this and many other kinds of shadows using Core Animations layers and the shadowPath property. The shadow that you are describing can be make with an elliptical shadow path.

enter image description here

The code to produce this shadow is below. You can tweak the size of the ellipse to have a rounder shape of the shadow. You can also tweak the position, opacity, color and blur radius using the shadow properties on the layer.

self.wantsLayer = YES;

NSView *viewWithRoundShadow = [[NSView alloc] initWithFrame:NSMakeRect(30, 30, 200, 100)];
[self addSubview:viewWithRoundShadow];

CALayer *backingLayer = viewWithRoundShadow.layer;
backingLayer.backgroundColor = [NSColor orangeColor].CGColor;

// Configure shadow
backingLayer.shadowColor   = [NSColor blackColor].CGColor;
backingLayer.shadowOffset  = CGSizeMake(0, -1.);
backingLayer.shadowRadius  = 5.0;
backingLayer.shadowOpacity = 0.75;

CGRect shadowRect = backingLayer.bounds;
CGFloat shadowRectHeight = 25.;
shadowRect.size.height = shadowRectHeight;
// make narrow
shadowRect = CGRectInset(shadowRect, 5, 0);

backingLayer.shadowPath = CGPathCreateWithEllipseInRect(shadowRect, NULL);

Just to show some examples of other shadows than can be created using the same technique; a path like this

enter image description here

will produce a shadow like this

enter image description here

like image 129
David Rönnqvist Avatar answered Nov 15 '22 09:11

David Rönnqvist