Skip to content

Instantly share code, notes, and snippets.

@brandenbyers
Created March 3, 2020 06:43
Show Gist options
  • Save brandenbyers/123b4978c1be00d72f908c84a53229cb to your computer and use it in GitHub Desktop.
Save brandenbyers/123b4978c1be00d72f908c84a53229cb to your computer and use it in GitHub Desktop.
Overlapping card shadows in SwiftUI
// Inspired by LearnSketch.com's video: https://youtu.be/xVREimb1Nnw
import SwiftUI
struct ContentView: View {
var body: some View {
ZStack {
Rectangle()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.foregroundColor(Color(UIColor(red: 0.89, green: 0.92, blue: 0.93, alpha: 1.00)))
.edgesIgnoringSafeArea(.all)
RoundedRectangle(cornerRadius: 10)
.frame(width: 200, height:200)
.offset(x: -40, y: -40)
.shadow(color: Color(red: 0.7, green: 0.7, blue: 0.7),
radius: 20, x: -1, y: 30)
RoundedRectangle(cornerRadius: 10)
.frame(width: 200, height:200)
.foregroundColor(Color(UIColor(red: 0.94, green: 0.95, blue: 0.96, alpha: 1.00)))
.offset(x: 40, y: 40)
.shadow(color: Color(red: 0.8, green: 0.8, blue: 0.8),
radius: 12, x: -0.50, y: 20)
RoundedRectangle(cornerRadius: 10)
.frame(width: 200, height:200)
.offset(x: -40, y: -40)
.shadow(color: Color(red: 0.8, green: 0.8, blue: 0.8),
radius: 6, x: -2, y: 6)
.mask(BottomRectangleMask())
RoundedRectangle(cornerRadius: 10)
.frame(width: 200, height:200)
.foregroundColor(.white)
.offset(x: -40, y: -40)
}
}
struct BottomRectangleMask: View {
var body: some View {
RoundedRectangle(cornerRadius: 10)
.frame(width: 200, height:200)
.offset(x: 40, y: 40)
}
}
}
@brandenbyers
Copy link
Author

Screenshot:

overlapping-card-shadows

I think the bottom shadow radius needs tweaking (the lighting doesn't seem right), but this gives a rough draft idea of the concept. The key here being the BottomRectangleMask(). Here is a screenshot making the mask more obvious:

obvious-mask

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment