简单封装了毛玻璃背景弹窗视图,并用闭包判断点击事件
SXAlertView.swift
import UIKit
// 闭包
typealias SXAlertViewBlock = (btnName: String) -> Void
class SXAlertView: UIView {
var leftLeave: Bool!
var alertTitleLabel: UILabel!
var alertContentLabel: UILabel!
var leftButton: UIButton!
var rightButton: UIButton!
var bgImageView: UIView!
let alertWidth:CGFloat = 245
let alertHeight:CGFloat = 160
let titleYOffset:CGFloat = 15
let titleHeight:CGFloat = 25
let contentOffset:CGFloat = 30
let betWeenLabelOffset:CGFloat = 20
var callBack: SXAlertViewBlock?
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func initWithTitle(title: String, content: String, leftTitle: String, rigthTitle: String) {
// alertView
self.layer.cornerRadius = 5.0
self.backgroundColor = UIColor.whiteColor()
//1 标题
alertTitleLabel = UILabel(frame: CGRect(x: 0, y: titleYOffset, width: alertWidth, height: titleHeight))
alertTitleLabel.text = title
alertTitleLabel.textAlignment = .Center
self.addSubview(alertTitleLabel)
//2 内容
let contentLabelWidth = alertWidth - 16
alertContentLabel = UILabel(frame: CGRect(x: (alertWidth - contentLabelWidth) * 0.5, y: CGRectGetMaxY(alertTitleLabel.frame), width: contentLabelWidth, height: 60))
alertContentLabel.text = content
alertContentLabel.numberOfLines = 0
alertContentLabel.textAlignment = .Center
alertContentLabel.textColor = UIColor(red: 127/255, green: 127/255, blue: 127/255, alpha: 1)
alertContentLabel.font = UIFont.systemFontOfSize(15)
self.addSubview(alertContentLabel)
var leftButtonFrame: CGRect!
var rightButtonFrame: CGRect!
let singleButtonWidth:CGFloat = 160
let coupleButtonWidth:CGFloat = 107
let buttonHeight:CGFloat = 40
let buttonBottomOffset:CGFloat = 10
// 判断左视图是否为空时、进行调整按钮尺寸
if leftTitle == "" {
rightButtonFrame = CGRect(x: (alertWidth - singleButtonWidth) * 0.5, y: alertHeight - buttonBottomOffset - buttonHeight, width: singleButtonWidth, height: buttonHeight)
rightButton = UIButton(type: .Custom)
rightButton.frame = rightButtonFrame
} else {
leftButtonFrame = CGRect(x: (alertWidth - 2 * coupleButtonWidth - buttonBottomOffset) * 0.5, y: alertHeight - buttonBottomOffset - buttonHeight, width: coupleButtonWidth, height: buttonHeight)
rightButtonFrame = CGRect(x: CGRectGetMaxX(leftButtonFrame) + buttonBottomOffset, y: alertHeight - buttonBottomOffset - buttonHeight, width: coupleButtonWidth, height: buttonHeight)
leftButton = UIButton(type: .Custom)
rightButton = UIButton(type: .Custom)
leftButton.frame = leftButtonFrame
rightButton.frame = rightButtonFrame
//3 左按钮
let leftBgColor = UIColor(red: 21/255, green: 173/255, blue: 158/255, alpha: 1)
leftButton.setBackgroundImage(imageWithColor(leftButtonFrame, color: leftBgColor), forState: .Normal)
leftButton.setTitle(leftTitle, forState: .Normal)
leftButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
leftButton.titleLabel?.font = UIFont.systemFontOfSize(14)
leftButton.addTarget(self, action: "leftButtonClick", forControlEvents: .TouchUpInside)
leftButton.layer.masksToBounds = true
leftButton.layer.cornerRadius = 5
self.addSubview(leftButton)
}
//4 右按钮
let rightBgColor = UIColor(red: 227/255, green: 100/255, blue: 83/255, alpha: 1)
rightButton.setBackgroundImage(imageWithColor(rightButtonFrame, color: rightBgColor), forState: .Normal)
rightButton.setTitle(rigthTitle, forState: .Normal)
rightButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
rightButton.titleLabel?.font = UIFont.systemFontOfSize(14)
rightButton.addTarget(self, action: "rightButtonClick", forControlEvents: .TouchUpInside)
rightButton.layer.masksToBounds = true
rightButton.layer.cornerRadius = 5
self.addSubview(rightButton)
// 右上角 按钮
let xBtn = UIButton(type: .Custom)
xBtn.frame = CGRect(x: alertWidth - 28, y: 4, width: 22, height: 22)
let xBtnBgColor = UIColor(red: 161/255, green: 163/255, blue: 166/255, alpha: 0.7)
xBtn.setBackgroundImage(imageWithColor(xBtn.frame, color: xBtnBgColor), forState: .Normal)
xBtn.setTitle("X", forState: .Normal)
xBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
xBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
xBtn.layer.masksToBounds = true
xBtn.layer.cornerRadius = 11
self.addSubview(xBtn)
xBtn.addTarget(self, action: "xBtnClick", forControlEvents: .TouchUpInside)
}
// MARK: - Aciton
func leftButtonClick() {
leftLeave = true
self.callBack!(btnName: "leftButton")
self.removeFromSuperview()
}
func rightButtonClick() {
leftLeave = false
self.removeFromSuperview()
self.callBack!(btnName: "rightButton")
}
func xBtnClick() {
self.callBack!(btnName: "xBtn")
self.removeFromSuperview()
}
func showAlertView() {
let shareWindow = UIApplication.sharedApplication().keyWindow
self.frame = CGRect(x: (shareWindow?.bounds.size.width)! - alertWidth * 0.5, y: -alertHeight - 30, width: alertWidth, height: alertHeight)
shareWindow?.addSubview(self)
}
override func removeFromSuperview() {
bgImageView.removeFromSuperview()
bgImageView = nil
let shareWindow = UIApplication.sharedApplication().keyWindow
let afterFrame = CGRect(x: ((shareWindow?.bounds.size.width)! - alertWidth) * 0.5, y: (shareWindow?.bounds.size.height)!, width: alertWidth, height: alertHeight)
// 移除 view 动画效果
UIView.animateWithDuration(0.35, delay: 0, options: .CurveEaseInOut, animations: { () -> Void in
self.frame = afterFrame
switch self.leftLeave {
case nil : // 右上角按钮
self.transform = CGAffineTransformMakeRotation(-CGFloat(M_1_PI) / 2)
case true: // 左边按钮
self.transform = CGAffineTransformMakeRotation(CGFloat(M_1_PI) / 1.5)
case false: // 右边按钮
self.transform = CGAffineTransformMakeRotation(CGFloat(M_1_PI) / 1.5)
default: // 其他
self.transform = CGAffineTransformMakeRotation(CGFloat(M_1_PI) / 1.5)
}
}) { (finished) -> Void in
super.removeFromSuperview()
}
}
override func willMoveToSuperview(newSuperview: UIView?) {
if newSuperview == nil {
return
}
let shareWindow = UIApplication.sharedApplication().keyWindow
let blurEffect = UIBlurEffect(style: .Light) // 模糊样式
let effectView = UIVisualEffectView(effect: blurEffect)
effectView.frame = shareWindow!.bounds
bgImageView = effectView
shareWindow?.addSubview(bgImageView)
self.transform = CGAffineTransformMakeRotation(-CGFloat(M_1_PI) / 2)
let afterFrame = CGRect(x: ((shareWindow?.bounds.size.width)! - alertWidth) * 0.5, y: ((shareWindow?.bounds.size.height)! - alertHeight) * 0.5, width: alertWidth, height: alertHeight)
UIView.animateWithDuration(0.35, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: .CurveEaseIn, animations: { () -> Void in
self.transform = CGAffineTransformMakeRotation(0)
self.frame = afterFrame
}) { (finished) -> Void in
super.willMoveToSuperview(newSuperview)
}
}
// 颜色转图片
func imageWithColor(imageFrame:CGRect, color:UIColor) -> UIImage {
let rect = CGRect(x: 0, y: 0, width: imageFrame.width, height: imageFrame.height)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
使用例子:
ViewController.swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let btn1 = UIButton(frame: CGRect(x: self.view.bounds.width/2 - 10, y: 50, width: 22, height: 22))
btn1.setTitle("+", forState: .Normal)
btn1.setTitleColor(UIColor.whiteColor(), forState: .Normal)
btn1.titleLabel?.font = UIFont.systemFontOfSize(22)
btn1.layer.masksToBounds = true
btn1.layer.cornerRadius = 11
btn1.backgroundColor = UIColor(red: 161/255, green: 163/255, blue: 166/255, alpha: 0.7)
btn1.addTarget(self, action: "btn1Click:", forControlEvents: .TouchUpInside)
self.view.addSubview(btn1)
let btn2 = UIButton(frame: CGRect(x: 50, y: 350, width: 22, height: 22))
btn2.setTitle("+", forState: .Normal)
btn2.setTitleColor(UIColor.whiteColor(), forState: .Normal)
btn2.titleLabel?.font = UIFont.systemFontOfSize(22)
btn2.layer.masksToBounds = true
btn2.layer.cornerRadius = 11
btn2.backgroundColor = UIColor(red: 161/255, green: 163/255, blue: 166/255, alpha: 0.7)
btn2.addTarget(self, action: "btn2Click:", forControlEvents: .TouchUpInside)
self.view.addSubview(btn2)
}
// btn1
func btn1Click(sender: UIButton) {
let alert = SXAlertView()
alert.initWithTitle("活着", content: "生活不只眼前的苟且", leftTitle: "是的", rigthTitle: "不是")
alert.showAlertView()
//通过闭包实现不同按钮点击事件
alert.callBack = ({(btnName: String) -> Void in
switch btnName {
case "leftButton":
print("点击了leftButton")
case "rightButton":
print("点击了rightButton")
case "xBtn":
print("点击了xBtn")
default:
break
}
})
}
// btn2
func btn2Click(sender: UIButton) {
let alert = SXAlertView()
alert.initWithTitle("亲", content: "喜欢就点个👍", leftTitle: "", rigthTitle: "喜欢")
alert.showAlertView()
//通过闭包实现不同按钮点击事件
alert.callBack = ({(btnName: String) -> Void in
switch btnName {
case "rightButton":
print("点击了rightButton")
case "xBtn":
print("点击了xBtn")
default:
break
}
})
}
}
网友评论