iOS

[iOS] swift shadow 그림자 적용 (x, y, blur, spread) feat. Zeplin

Kim_Baechu 2022. 1. 7. 18:20

제플린 등에서 제공하는 그림자 요소들을 쉽게 swiftf로 만들어주는 extension입니다.

extension CALayer {
    func applySketchShadow(
        color: UIColor,
        alpha: Float,
        x: CGFloat,
        y: CGFloat,
        blur: CGFloat,
        spread: CGFloat
    ) {
        masksToBounds = false
        shadowColor = color.cgColor
        shadowOpacity = alpha
        shadowOffset = CGSize(width: x, height: y)
        shadowRadius = blur / UIScreen.main.scale
        if spread == 0 {
            shadowPath = nil
        } else {
            let rect = bounds.insetBy(dx: -spread, dy: -spread)
            shadowPath = UIBezierPath(rect: rect).cgPath
        }
    }
}

예시

Swift

layer.applySketchShadow(color: .black, alpha: 0.3, x: 0, y: 0, blur: 13, spread: 0)