如何在Swift 4中的UIView(右,左,下)中创建三个侧面的阴影?

时间:2018-12-28 11:57:08

标签: uiview swift4 shadow

我尝试创建UIView(右,左,下)的阴影,但未创建确切的阴影。如何创建UIView的阴影(如下图所示)?请帮帮我!

Shadow image

1 个答案:

答案 0 :(得分:0)

您可以使用UIBezierPath向视图添加阴影,如下所示:

extension UIVIew{
     func applyDropShadow(scale: Bool = true) {
        layer.masksToBounds = false
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOpacity = 0.5
        layer.shadowOffset = CGSize(width: -1, height: 1)
        layer.shadowRadius = 1

        layer.shadowPath = UIBezierPath(rect: bounds).cgPath
        layer.shouldRasterize = true
        layer.rasterizationScale = scale ? UIScreen.main.scale : 1
      }
}

调整shadowOffset以获得设计阴影位置。