官网文档 protocol
该协议可以通过一个类,结构或枚举 定义这些需要的方法
首先我们创建一个UIView 来实现AlertView 的载体
命名为 SwiftCustomAlertView.swift
首先我们假设AlertView 有两个按钮,确定和取消
我们要实现这两个按钮的协议来通知控制器我们点击了哪个按钮。如下
下面就具体如何实现AlertView 界面 写得都非常简单一看就明白,我就不具体说明了,具体思路掌握了,就可以任意自定义控件了
@objc protocol SwiftCustomAlertViewDelegate : NSObjectProtocol{
optional func selectOkButtonalertView()
optional func selecttCancelButtonAlertView()
}
optional 操作符来指定协议可以不实现
继承 NSObjectProtocol 可以用来后面调用respondsToSelector 方法检测是否实现了该delegate
如果需要传值可以在方法里添class
SwiftCustomAlertView: UIView {
private let defaultWidth = 280.0 //默认Alert宽度
private let defaultHeight = 146.0 //默认Alert高度
private let defaultCornerRadius = 5.0 //默认Alert 圆角度数
private var viewY:Double!
private var viewWidth: Double!
private var viewHeight: Double!
private var cancelButtonTitle: String?
private var oKButtonTitle: String?
private var cancelButton: UIButton?
private var oKButton: UIButton?
private var title: String?
private var message: String?
private var titleLabel: UILabel!
private var messageLabel: UILabel!
var cornerRadius: Double!
weak var delegate: SwiftCustomAlertViewDelegate? // delegate
//初始化
init(title: String?, message: String?, delegate: SwiftCustomAlertViewDelegate?) {
super.init(frame: CGRect(x: 0, y: 0, width: defaultWidth, height: defaultHeight))
setup(title, message: message,delegate: delegate)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//设置相关数据
private func setup(title: String?, message: String?,delegate: SwiftCustomAlertViewDelegate?) {
self.title = title
self.message = message
self.delegate = delegate
self.setUpDefaultValue()
self.setUpElements()
}
//默认参数
private func setUpDefaultValue() {
clipsToBounds = true
cancelButtonTitle = "取消"
oKButtonTitle = "确定"
viewWidth = defaultWidth
viewHeight = defaultHeight
cornerRadius = defaultCornerRadius
layer.cornerRadius = CGFloat(cornerRadius)
self.backgroundColor = UIColor.redColor()
}
//设置相关ui
private func setUpElements() {
titleLabel = UILabel(frame: CGRectZero)
messageLabel = UILabel(frame: CGRectZero)
if title != nil {
titleLabel.text = title
titleLabel.numberOfLines = 0
titleLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
titleLabel.textColor = UIColor.blackColor()
titleLabel.font = UIFont.boldSystemFontOfSize(17)
titleLabel.textAlignment = NSTextAlignment.Center
titleLabel.backgroundColor = UIColor.clearColor()
addSubview(titleLabel)
}
if message != nil {
messageLabel.text = message
messageLabel.numberOfLines = 0
messageLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
messageLabel.textColor = UIColor.blackColor()
messageLabel.font = UIFont.systemFontOfSize(13)
messageLabel.textAlignment = NSTextAlignment.Center
messageLabel.backgroundColor = UIColor.clearColor()
addSubview(messageLabel)
}
if let cancelTitle = cancelButtonTitle {
cancelButton = UIButton(type: UIButtonType.Custom)
cancelButton!.setTitle(cancelTitle, forState: UIControlState.Normal)
cancelButton!.backgroundColor = UIColor.blueColor()
cancelButton!.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
cancelButton!.titleLabel?.font = UIFont.boldSystemFontOfSize(17)
cancelButton?.tag = 9
addSubview(cancelButton!)
}
if let okTitle = oKButtonTitle {
oKButton = UIButton(type: UIButtonType.Custom)
oKButton!.setTitle(okTitle, forState: UIControlState.Normal)
oKButton!.backgroundColor = UIColor.yellowColor()
oKButton!.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
oKButton!.titleLabel?.font = UIFont.boldSystemFontOfSize(17)
oKButton?.tag = 10
addSubview(oKButton!)
}
}
private func layoutFrameshowing() {
cancelButton!.addTarget(self, action: Selector("cancelButtonClicked:"), forControlEvents: UIControlEvents.TouchUpInside)
cancelButton!.frame = CGRect(x: viewWidth/2, y: viewHeight-40, width: viewWidth/2, height: 40)
oKButton!.addTarget(self, action: Selector("okButtonClicked:"), forControlEvents: UIControlEvents.TouchUpInside)
oKButton!.frame = CGRect(x:0, y: viewHeight-40, width: viewWidth/2, height: 40)
if title != nil {
titleLabel.frame = CGRect(x: 10, y: 5, width: viewWidth - 20, height: 20)
}
if message != nil {
messageLabel.frame = CGRect(x: 10, y: 0, width: viewWidth - 20, height: 0)
labelHeightToFit(messageLabel)
}
if message != nil {
messageLabel.center = CGPoint(x: viewWidth/2, y: 5 + Double(titleLabel.frame.size.height) + 20 + Double(messageLabel.frame.size.height)/2)
}
}
private func labelHeightToFit(label: UILabel) {
let maxWidth = label.frame.size.width - 20
let maxHeight : CGFloat = 500
let rect = label.attributedText?.boundingRectWithSize(CGSizeMake(maxWidth, maxHeight),
options: .UsesLineFragmentOrigin, context: nil)
var frame = label.frame
frame.size.height = rect!.size.height
label.frame = frame
}
func cancelButtonClicked(button: UIButton) {
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.center = CGPoint(x: -self.viewWidth , y:self.viewY + self.viewHeight/2)
}) { (Bool) -> Void in
self.removeFromSuperview()
}
if delegate?.respondsToSelector(Selector("selecttCancelButtonAlertView")) == true {
print("cancelDelegate")
delegate?.selecttCancelButtonAlertView!()
}
}
func okButtonClicked(button: UIButton) {
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.center = CGPoint(x:Double(UIScreen .mainScreen().bounds.size.width)+self.viewWidth , y:self.viewY + self.viewHeight/2)
}) { (Bool) -> Void in
self.removeFromSuperview()
}
if delegate?.respondsToSelector(Selector("selectOkButtonalertView")) == true {
delegate?.selectOkButtonalertView!()
}
}
func show() {
if let window: UIWindow = UIApplication.sharedApplication().keyWindow {
show(window)
}
}
func show(view: UIView) {
layoutFrameshowing()
self.viewY = (Double(view.frame.size.height) - viewHeight)/2
self.frame = CGRect(x: (Double(view.frame.size.width) - viewWidth)/2, y: viewY, width: viewWidth, height: viewHeight)
view.addSubview(self)
view.bringSubviewToFront(self)
self.alpha = 0.3
UIView.animateWithDuration(0.2, delay: 0.0, options: .CurveEaseOut, animations: {
self.alpha = 1;
self.transform = CGAffineTransformMakeScale(0.8, 0.8)
}, completion: { finished in
UIView.animateWithDuration(0.2, animations: {() -> Void in
self.transform = CGAffineTransformMakeScale(1.2, 1.2)
}) { (Bool) -> Void in
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.transform = CGAffineTransformMakeScale(1, 1)
})
}
})
}
}
在调用的地方
let alertView = SwiftCustomAlertView(title:"swift", message:"custom swift alert", delegate: self)
alertView .show();
和实现delegate方法
func selectOkButtonalertView() {
}
func selecttCancelButtonAlertView() {
}