์—…๋ฌด ํ•˜๋ฉด์„œ Shadow์— ๋Œ€ํ•ด ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ๋‹ค.

์ฝ”๋“œ

extension CALayer {
    func shadow(color: UIColor, alpha: CGFloat, x: CGFloat, y: CGFloat, blur: CGFloat, spread: CGFloat) {
        shadowColor = color.cgColor
        shadowOpacity = alpha
        shadowOffset = CGSize(width: x, height: y)
        shadowRadius = blur
        masksToBounds = false
        if properties.spread == 0 {
            shadowPath = nil
        } else {
            let dx = -spread
            let rect = bounds.insetBy(dx: dx, dy: dx)
            shadowPath = UIBezierPath(rect: rect).cgPath
        }
    }
}
 
self.view.layer.shadow(color: .black, alpha: 0.5, x: 0, y: 0, blur: 4, spread: 0)

Subview์˜ ์ˆœ์„œ๋ฅผ ์ž˜ ๋„ฃ์ž.

  • Shadow ์ ์šฉ์€ addSubview ์ˆœ์„œ์— ๋”ฐ๋ผ ๊ฐ€๋ ค์ง์ด ๊ฒฐ์ •๋œ๋‹ค.
  • ๊ฐ€์žฅ ๋‚˜์ค‘์— ๋„ฃ์€ ๊ฒƒ์ด ๊ฐ€์žฅ ์œ„์— ์žˆ๋‹ค๊ณ  ํŒ๋‹จ๋˜์–ด ๊ทธ๋ฆผ์ž๊ฐ€ ์ƒ๊ธด๋‹ค.
  • ์ˆœ์„œ ์ž˜๋ชป ๋„ฃ์œผ๋ฉด ๊ทธ๋ฆผ์ž๊ฐ€ ๊ฐ€๋ ค์ ธ์„œ ์•ˆ๋ณด์ด๊ฒŒ ๋œ๋‹ค.
  • ์ฆ‰, ๊ทธ๋ฆผ์ž์— ๋Œ€ํ•ด์„œ๋Š” ๊ตํ™˜๋ฒ•์น™์ด ์„ฑ๋ฆฝํ•˜์ง€ ์•Š๋Š”๋‹ค.

๋ฐฐ๊ฒฝ์ƒ‰์„ ๋„ฃ์ž.

  • ๋˜ํ•œ ํŠน์ • View Component์˜ background๊ฐ€ ์ •ํ•ด์ง€์ง€ ์•Š์•˜๋‹ค๋ฉด, ๋น›์€ ํˆฌ๊ณผํ•œ๋‹ค.
  • ๊ทธ๋ž˜์„œ ํŠน์ • View์•ˆ์— ๋‹ค๋ฅธ View๋ฅผ ๋„ฃ์—ˆ๋Š”๋ฐ, ๋ฐ”๊นฅ View์˜ ์ƒ‰์€ ์ง€์ •ํ•˜์ง€ ์•Š์€ ์ƒํƒœ, ๋‚ด๋ถ€ View์˜ ์ƒ‰์€ ์ง€์ •ํ•œ ์ƒํƒœ๋กœ ๊ทธ๋ฆผ์ž๋ฅผ ๋“œ๋ฆฌ์šฐ๋ฉด
  • ๋‚ด๋ถ€ View๋งŒ ๊ทธ๋ฆผ์ž๊ฐ€ ์ƒ๊ธฐ๊ณ  ๋ฐ”๊นฅ๋ทฐ๋Š” ์ƒ๊ธฐ์ง€ ์•Š๋Š”๋‹ค.

Blur

  • ๊ทธ๋ฆผ์ž ํ…Œ๋‘๋ฆฌ์˜ ํ๋ฆฟํ•œ ์ •๋„๋ฅผ ๋งํ•œ๋‹ค.
  • blur๋ฅผ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ์˜ ๋ฐฉ๋ฒ•์œผ๋กœ ์ ์šฉํ•ด์•ผ ํ•œ๋‹ค๊ณ  ์•Œ๋ ค์ฃผ๋”๋ผ.
  • blur / UIScreen.main.scale
  • ๊ทธ๋Ÿฐ๋ฐ shadowRadius๋ฅผ ๋ณด๋ฉด, ํ•ด๋‹น ๊ฐ’์€ ์ด๋ฏธ pt๋ผ๊ณ  ๋ช…์‹œ๋˜์–ด ์žˆ๋‹ค.
  • ๋”ฐ๋ผ์„œ ๊ตณ์ด ๋ณ€ํ™˜๊ณผ์ •์„ ๊ฑฐ์น˜์ง€ ์•Š๊ณ , ์ผ๋ฐ˜์ ์ธ ๋””์ž์ธ์—์„œ pt๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์—
  • ๊ทธ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ด๋„ ๋ฌธ์ œ๊ฐ€ ์—†์„ ๊ฒƒ์œผ๋กœ ์ƒ๊ฐ๋œ๋‹ค.
  • spread๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค.

Spread

Reference