在 iOS 里 100% 还原 Sketch 实现的阴影效果(转载:https://juejin.im/post/5dd4cd71f265da0bf80b5820)

实际项目中也有卡片阴影的使用

extension CALayer {
    func skt_setShadow(color: UIColor? = .black,
                       alpha: CGFloat = 0.5,
                       x: CGFloat = 0, y: CGFloat = 2,
                       blur: CGFloat = 4,
                       spread: CGFloat = 0) {
        shadowOffset = CGSize(width: x, height: y)
        shadowRadius = blur * 0.5
        shadowColor = color?.cgColor
        shadowOpacity = Float(alpha)

        let rect = bounds.insetBy(dx: -spread, dy: -spread)
        let path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
        shadowPath = path.cgPath
    }
}

使用:

layer.skt_setShadow(color: .black, alpha: 0.5,
                    x: 0, y: 10, 
                    blur: 20, 
                    spread: 10)

你可能感兴趣的:(在 iOS 里 100% 还原 Sketch 实现的阴影效果(转载:https://juejin.im/post/5dd4cd71f265da0bf80b5820))