Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Created April 5, 2019 02:15
Show Gist options
  • Save KentarouKanno/1f5dd33faa91fc874e95a6c52acf0430 to your computer and use it in GitHub Desktop.
Save KentarouKanno/1f5dd33faa91fc874e95a6c52acf0430 to your computer and use it in GitHub Desktop.

☆ SketchのShadowをコードで同じ様に設定できるようにするExtension

extension CALayer {
    
    func applySketchShadow(
        color: UIColor = .black,
        alpha: Float = 0.5,
        x: CGFloat = 0,
        y: CGFloat = 2,
        blur: CGFloat = 4,
        spread: CGFloat = 0) {
        
        shadowColor = color.cgColor
        shadowOpacity = alpha
        shadowOffset = CGSize(width: x, height: y)
        shadowRadius = blur / 2.0
        if spread == 0 {
            shadowPath = nil
        } else {
            let dx = -spread
            let rect = bounds.insetBy(dx: dx, dy: dx)
            shadowPath = UIBezierPath(rect: rect).cgPath
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment