问题图像截图
class ViewController: UIViewController {
var shadow : UIView!
override func viewDidLoad() {
super.viewDidLoad()
shadow = UIView(frame: CGRect(x: 50,y: 50,width: 150,height:150))
shadow.backgroundColor = .red
shadow.dropShadow()
self.view.addSubview(shadow)
}
@IBAction func btnActn(_ sender: Any) {self.shadow.frame = CGRect(x: 50,y: 50,width: 150,height: 50)
}
}
extension UIView {
func dropShadow(scale: Bool = true) {
layer.masksToBounds = false
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.5
layer.shadowOffset = CGSize(width: 1, height: 1)
layer.shadowRadius = 2
layer.shadowPath = UIBezierPath(rect: bounds).cgPath
layer.shouldRasterize = true
layer.rasterizationScale = scale ? UIScreen.main.scale …
Run Code Online (Sandbox Code Playgroud)