代码如下:
let testView = UIView(frame: CGRect(x: self.view.frame.width/2 - 100, y: self.view.frame.height/2 - 100, width: 200, height: 200))
testView.backgroundColor = .yellow
///The opacity of the layer’s shadow. Animatable.
testView.layer.shadowOpacity = 0.5
///The color of the layer’s shadow. Animatable.
testView.layer.shadowColor = UIColor.black.cgColor
///The offset (in points) of the layer’s shadow. Animatable.
testView.layer.shadowOffset = CGSize(width: 0, height: 0)
///The blur radius (in points) used to render the layer’s shadow. Animatable.
testView.layer.shadowRadius = 50
备注:设置了阴影的同时,切记不要设置视图的clipsToBounds为true,否则阴影失效