Skip to content

Instantly share code, notes, and snippets.

@HarshilShah
Created October 15, 2024 07:52
Show Gist options
  • Save HarshilShah/2fb0db63bdfd6d6373c8feaf56a3f252 to your computer and use it in GitHub Desktop.
Save HarshilShah/2fb0db63bdfd6d6373c8feaf56a3f252 to your computer and use it in GitHub Desktop.
A ShapeStyle extension to generate smooth shadows
import SwiftUI
extension ShapeStyle {
// Based on https://gist.github.com/sophiateutschler/54c0e6ca490fee658e303a63487dc382
func smoothShadow(
color: Color = Color(white: 0.0, opacity: 1.0 / 3.0),
layers: Int = 3,
curve: UnitCurve = .easeInOut,
radius baseRadius: CGFloat,
x baseX: CGFloat = 0.0,
y baseY: CGFloat = 0.0
) -> some ShapeStyle {
var result = AnyShapeStyle(self)
for currentLayer in 0 ..< layers {
let progress = CGFloat(currentLayer) / CGFloat(layers)
let radius = baseRadius * curve.value(at: progress)
let x = baseX * curve.value(at: progress)
let y = baseY * curve.value(at: progress)
result = AnyShapeStyle(result.shadow(.drop(color: color, radius: radius, x: x, y: y)))
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment