swift2.0 protocol 实战自定义UIAlertView

swift2.0 protocol 实战自定义UIAlertView,第1张

概述官网文档 protocol 该协议可以通过一个类,结构或枚举 定义这些需要的方法 首先我们创建一个UIView 来实现AlertView 的载体 命名为 SwiftCustomAlertView.swift 首先我们假设AlertView 有两个按钮,确定和取消 我们要实现这两个按钮的协议来通知控制器我们点击了哪个按钮。如下 下面就具体如何实现AlertView 界面 写得都非常简单一看就明白,我

官网文档 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?,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,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,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 @H_319_301@in self.center = CGPoint(x: -self.vIEwWIDth,y:self.vIEwY + self.vIEwHeight/2) }) { (Bool) -> VoID @H_319_301@in self.removeFromSupervIEw() } if delegate?.respondsToSelector(Selector("selecttCancelbuttonAlertVIEw")) == true { print("cancelDelegate") delegate?.selecttCancelbuttonAlertVIEw!() } } func okbuttonClicked(button: UIbutton) { UIVIEw.animateWithDuration(0.5,animations: { () -> VoID @H_319_301@in self.center = CGPoint(x:Double(UIScreen .mainScreen().bounds.size.wIDth)+self.vIEwWIDth,y:self.vIEwY + self.vIEwHeight/2) }) { (Bool) -> VoID @H_319_301@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 @H_319_301@in UIVIEw.animateWithDuration(0.2,animations: {() -> VoID @H_319_301@in self.transform = CGAffinetransformMakeScale(1.2,1.2) }) { (Bool) -> VoID @H_319_301@in UIVIEw.animateWithDuration(0.1,animations: { () -> VoID @H_319_301@in self.transform = CGAffinetransformMakeScale(1,1) }) } }) } }

在调用的地方

@H_319_301@let alertVIEw = SwiftCustomAlertVIEw(Title:"swift",message:"custom swift alert",@H_319_301@delegate: self)      alertVIEw .show();

和实现delegate方法

func selectOkbuttonalertVIEw() { }  func selecttCancelbuttonAlertVIEw() { }
总结

以上是内存溢出为你收集整理的swift2.0 protocol 实战自定义UIAlertView全部内容,希望文章能够帮你解决swift2.0 protocol 实战自定义UIAlertView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1083805.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存