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
我想在我的UIVIew中实现完全相同的阴影。当我使用上面的代码运行我的应用程序时,它分布在4个方面。注意:阴影在两边都很薄,在中心处是粗体。
答案 0 :(得分:6)
尝试创建自定义路径:
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
这会产生类似的结果,尝试自定义积分位置和其他值以满足您的需求。
答案 1 :(得分:3)
稍微改变@jonaszmclaren给出一个矩形阴影:
func makeBottomShadow(forView view: UIView, shadowHeight: CGFloat = 5) {
let shadowPath = UIBezierPath()
shadowPath.move(to: CGPoint(x: 0, y: view.bounds.height))
shadowPath.addLine(to: CGPoint(x: view.bounds.width, y: view.bounds.height))
shadowPath.addLine(to: CGPoint(x: view.bounds.width, y: view.bounds.height + shadowHeight))
shadowPath.addLine(to: CGPoint(x: 0, y: view.bounds.height + shadowHeight))
shadowPath.close()
view.layer.shadowColor = UIColor.darkGray.cgColor
view.layer.shadowOpacity = 0.5
view.layer.masksToBounds = false
view.layer.shadowPath = shadowPath.cgPath
view.layer.shadowRadius = 2
}
答案 2 :(得分:0)
试一试
<强> IBOutlet中强>
@IBOutlet weak var vwShadow: UIView!
在ViewdidLoad
中vwShadow.layer.shadowColor = UIColor.gray.cgColor
vwShadow.layer.masksToBounds = false
vwShadow.layer.shadowOffset = CGSize(width: 0.0 , height: 5.0)
vwShadow.layer.shadowOpacity = 1.0
vwShadow.layer.shadowRadius = 1.0