如何在Swift 3中为UIView提供底影?

Alw*_*win 3 uiview ios swift swift3

在此输入图像描述

这就是我想要的

在此输入图像描述

我的守则

let shadowPath = UIBezierPath(rect: shadowView.bounds).cgPath
shadowView.layer.shadowColor = UIColor.grey.cgColor
shadowView.layer.shadowOffset = CGSize(width: 0, height: 1)
shadowView.layer.shadowOpacity = 0.5
shadowView.layer.masksToBounds = false
shadowView.layer.shadowPath = shadowPath
shadowView.layer.shadowRadius = 5
Run Code Online (Sandbox Code Playgroud)

我想在我的UIVIew中实现完全相同的阴影.当我使用上面的代码运行我的应用程序时,它分布在4个方面.注意:阴影在两边都很薄,在中心处是粗体.

jon*_*ren 7

尝试创建自定义路径:

let shadowPath = UIBezierPath()
shadowPath.move(to: CGPoint(x: someView.bounds.origin.x, y: someView.frame.size.height))
shadowPath.addLine(to: CGPoint(x: someView.bounds.width / 2, y: someView.bounds.height + 7.0))
shadowPath.addLine(to: CGPoint(x: someView.bounds.width, y: someView.bounds.height))
shadowPath.close()

shadowView.layer.shadowColor = UIColor.darkGray.cgColor
shadowView.layer.shadowOpacity = 1
shadowView.layer.masksToBounds = false
shadowView.layer.shadowPath = shadowPath.cgPath
shadowView.layer.shadowRadius = 5
Run Code Online (Sandbox Code Playgroud)

这会给出类似的结果,尝试自定义点位置和其他值以满足您的需求.