Playing with UIView shadows in Swift

I saw a demo somewhere, where some dude created different nifty shadow effects by tweaking the shadowPath property of UIView’s CALayer. Being pretty new to Swift and iOS development, I figured it would be fun to play around with it – here’s the result (screenshot from the iPhone simulator):

four different shadow effects

Here’s how I did it – first, I installed this simple UIViewController to display the four images:

and then I created the applyPlainShadow method (which is basically the shadow example you’ll find everywhere when you Google for “UIView drop shadow”):

Simple and neat… – but now the fun begins – let’s create that convex-piece-of-paper-on-a-surface-effect that you’ll see in many places on the web – it’s created by simply using a UIBezierPath to draw the outline of the shadow, and then setting the layer’s shadowPath to the path’s CGRect:

Neat! Lastly, let’s create a blurry oval drop shadow, as if the view was hovering high over some low surface that expands into the screen… again, I’m using the shadowPath property, initializing the UIBezierPath with the overload that creates an oval shape from a rectangle:

Nice!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.