Skip to content

Instantly share code, notes, and snippets.

@chosa91
Created May 20, 2020 15:34
Show Gist options
  • Save chosa91/1ddae915aa19bae4ae632246773660a5 to your computer and use it in GitHub Desktop.
Save chosa91/1ddae915aa19bae4ae632246773660a5 to your computer and use it in GitHub Desktop.
import UIKit
// Source: https://sarunw.com/posts/match-view-shadow-to-sketch-shadow/
extension UIView {
func applySketchShadow(
color: UIColor = .black,
alpha: Float = 0.2,
x: CGFloat = 0,
y: CGFloat = 2,
blur: CGFloat = 4,
spread: CGFloat = 0
) {
layer.shadowColor = color.cgColor
layer.shadowOpacity = alpha
layer.shadowOffset = CGSize(width: x, height: y)
layer.shadowRadius = blur / 2
if spread == 0 {
layer.shadowPath = nil
} else {
let dx = -spread
let rect = bounds.insetBy(dx: dx, dy: dx)
layer.shadowPath = UIBezierPath(rect: rect).cgPath
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment