Created
May 20, 2020 15:34
-
-
Save chosa91/1ddae915aa19bae4ae632246773660a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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