// 绘制遮罩,定义宽高为屏幕的宽高
let maskView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
// 设置透明度
maskView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
// 添加点击事件,点击自身,移除
maskView.addTarget(self, action: #selector(hideMask))
// 给遮罩上添加一些内容
let logoutView = UIView(frame: CGRect(x: 0, y: self.view.frame.height - 80, width: self.view.frame.width, height: 80))
maskView.addSubview(logoutView)
logoutView.backgroundColor = UIColor.white
logoutView.layer.cornerRadius = 10
logoutView.layer.masksToBounds = true
// 将遮罩添加到屏幕上
UIApplication.shared.keyWindow?.addSubview(maskView)
// 移除遮罩事件
@objc func hideMask(uiTap: UITapGestureRecognizer) {
let view = uiTap.view
view?.removeFromSuperview()
}
网友评论