2018-05-07
- swift4.0
- snapkit
import UIKit
class SAlertView: UIView {
static var alert : SAlertView?
typealias Block = () -> Void
var selectBlock : Block?
var blackView : UIView!
var alertLab : UILabel!
var alertBgView : UIVisualEffectView!
var alertTitleLab : UILabel!
var alertCancelBtn : UIButton!
var alertConfirmBtn : UIButton!
var horizontalLine : UIView!
var verticalLine : UIView!
override init(frame: CGRect) {
super.init(frame: frame)
createLayouts()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//MARK: - 布局
func createLayouts() {
blackView = UIView()
blackView.backgroundColor = kRGBACOLOR(r: 65, g: 65, b: 65, a: 0.5)
addSubview(blackView)
alertBgView = UIVisualEffectView.init(effect: UIBlurEffect.init(style: .extraLight))
alertBgView.layer.cornerRadius = 10
alertBgView.layer.masksToBounds = true
addSubview(alertBgView)
alertLab = UILabel()
alertLab.font = UIFont.systemFont(ofSize: 16)
alertLab.textAlignment = .center
alertLab.numberOfLines = 10
addSubview(alertLab)
alertTitleLab = UILabel()
alertTitleLab.font = UIFont.boldSystemFont(ofSize: 18)
alertTitleLab.textAlignment = .center
addSubview(alertTitleLab)
alertCancelBtn = UIButton.init(type: .system)
alertCancelBtn.setTitleColor(.blue, for: .normal)
alertCancelBtn.setTitle("取消", for: .normal)
alertCancelBtn.addTarget(self, action: #selector(cancelAction(sender:)), for: .touchUpInside)
addSubview(alertCancelBtn)
alertConfirmBtn = UIButton.init(type: .system)
alertConfirmBtn.setTitleColor(.red, for: .normal)
alertConfirmBtn.setTitle("確定", for: .normal)
alertConfirmBtn.addTarget(self, action: #selector(comfirmAction(sender:)), for: .touchUpInside)
addSubview(alertConfirmBtn)
horizontalLine = UIView()
horizontalLine.backgroundColor = .lightGray
addSubview(horizontalLine)
verticalLine = UIView()
verticalLine.backgroundColor = .lightGray
addSubview(verticalLine)
blackView.snp.makeConstraints { (make) in
make.edges.equalTo(self.snp.edges)
}
alertLab.snp.makeConstraints { (make) in
make.center.equalTo(self.snp.center)
make.left.equalTo(50)
make.right.equalTo(-50)
make.height.greaterThanOrEqualTo(50).priority(.high)
}
alertTitleLab.snp.makeConstraints { (make) in
make.bottom.equalTo(alertLab.snp.top)
make.left.equalTo(50)
make.right.equalTo(-50)
make.height.equalTo(30)
}
alertCancelBtn.snp.makeConstraints { (make) in
make.top.equalTo(alertLab.snp.bottom).offset(5)
make.left.equalTo(40)
make.height.equalTo(40)
}
alertConfirmBtn.snp.makeConstraints { (make) in
make.top.equalTo(alertLab.snp.bottom).offset(5)
make.left.equalTo(alertCancelBtn.snp.right)
make.right.equalTo(-40)
make.width.equalTo(alertCancelBtn.snp.width)
make.height.equalTo(40)
}
alertBgView.snp.makeConstraints { (make) in
make.top.equalTo(alertTitleLab.snp.top).offset(-10)
make.left.equalTo(alertLab.snp.left).offset(-10)
make.right.equalTo(alertLab.snp.right).offset(10)
make.bottom.equalTo(alertConfirmBtn.snp.bottom).offset(0)
}
horizontalLine.snp.makeConstraints { (make) in
make.bottom.equalTo(alertCancelBtn.snp.top).offset(-0.5)
make.left.equalTo(40)
make.right.equalTo(-40)
make.height.equalTo(0.5)
}
verticalLine.snp.makeConstraints { (make) in
make.top.equalTo(alertCancelBtn.snp.top)
make.left.equalTo(alertCancelBtn.snp.right).offset(-0.25)
make.right.equalTo(alertConfirmBtn.snp.left).offset(0.25)
make.height.equalTo(40)
}
}
//MARK: - 展示
static func show(title: String, alertText: String, action: @escaping Block) {
if alert != nil {
alert?.removeFromSuperview()
alert = nil
}
alert = SAlertView()
alert?.selectBlock = action
alert?.alertTitleLab.text = title
alert?.alertLab.text = alertText
kAppDelegate?.window?.addSubview(alert!)
alert?.snp.makeConstraints { (make) in
make.edges.equalTo((kAppDelegate?.window?.snp.edges)!)
}
}
//MARK: - 隐藏
private func dismiss() {
SAlertView.alert?.removeFromSuperview()
SAlertView.alert = nil
}
//MARK: - 取消
@objc func cancelAction(sender: UIButton) {
dismiss()
}
//MARK: - 确定
@objc func comfirmAction(sender: UIButton) {
if let block = self.selectBlock {
block()
}
dismiss()
}
}
- example
SAlertView.show(title: "延迟通知", alertText: "邮件的延迟通知内容邮件的延迟通知内容"){
printLog("確定")
}
- display

网友评论