swift-自定义Alert

前言

1. 由于我的项目中经常需要用到各种提示框,但是苹果自带的不符合产品需求,所以自定义了一个Alert,当作练习之用。

2. 欢迎老鸟指正。

好了,附代码。

1.在Alert中需要用到自定义的button

1 let SW = UIScreen.mainScreen().bounds.size.width
2 
3 let SH = UIScreen.mainScreen().bounds.size.height

 

 1 class ZLButton: UIButton {
 2     typealias ChooseBlock = ZLButton -> Void
 3     var chooseBlock : ChooseBlock!
 4 
 5     func chooseClassify(block block:ChooseBlock) {
 6         chooseBlock=block
 7         self.addTarget(self, action: #selector(self.choose(button:)), forControlEvents: UIControlEvents.TouchUpInside)
 8     }
 9 
10     func choose(button button:ZLButton) {
11         self.chooseBlock(button)
12     }
13 }

2.自定义的Alert

  1 import UIKit
  2 
  3 class ZLAlert: UIView {
  4 
  5     var tap :UITapGestureRecognizer!
  6     var view1 :UIView!
  7     var view2 :UIView!
  8     var backGroundView :UIView!
  9 
 10     typealias FirstChoose = ZLButton -> Void
 11     var firstChoose : FirstChoose!
 12 
 13     typealias SecondChoose = ZLButton -> Void
 14     var secondChoose : SecondChoose!
 15 
 16     override init(frame: CGRect) {
 17         super.init(frame: frame)
 18         self.frame=frame
 19         let window = UIApplication.sharedApplication().keyWindow
 20         window?.addSubview(self)
 21         backGroundView = UIView(frame: frame)
 22         backGroundView.backgroundColor=UIColor.grayColor()
 23         backGroundView.alpha=0.5
 24     }
 25 //show展示在正中央的Alert
 26     func showCenterAlert(message message :String, value1 :String, block1 :FirstChoose, value2 :String, block2 :SecondChoose) {
 27         firstChoose=block1
 28         secondChoose=block2
 29 
 30         tap = UITapGestureRecognizer(target: self,action: #selector(hideCenterAlert))
 31         self.addGestureRecognizer(tap)
 32         self.addSubview(backGroundView)
 33 
 34         view1 = UIView()
 35         view1.center=backGroundView.center
 36         view1.bounds=CGRectMake(0, 0, SW-40/375.0*SW, 150/375.0*SW)
 37         view1.backgroundColor=UIColor.whiteColor()
 38         view1.layer.masksToBounds=true
 39         view1.layer.cornerRadius=10/375.0*SW
 40         self.addSubview(view1)
 41 
 42         let messageLab = UILabel(frame: CGRectMake(0, 0, SW-40/375.0*SW, 100/375.0*SW))
 43         messageLab.text=message
 44         messageLab.textAlignment=NSTextAlignment.Center
 45         messageLab.font=UIFont.systemFontOfSize(17/375.0*SW)
 46         messageLab.layer.borderWidth=0.5
 47         messageLab.layer.borderColor=RGB(230, g: 230, b: 230, a: 1).CGColor
 48         view1.addSubview(messageLab)
 49 
 50         let firstBtn = ZLButton(type: UIButtonType.System)
 51         firstBtn.setTitle(value1, forState: UIControlState.Normal)
 52         firstBtn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
 53         firstBtn.frame=CGRectMake(0, 100/375.0*SW, SW/2-20/375.0*SW, 50/375.0*SW)
 54         firstBtn.titleLabel?.font=UIFont.systemFontOfSize(15/375.0*SW)
 55         firstBtn.chooseClassify { (button) in
 56             self.firstChoose(button)
 57         }
 58         view1.addSubview(firstBtn)
 59 
 60         let secondBtn = ZLButton(type: UIButtonType.System)
 61         secondBtn.setTitle(value2, forState: UIControlState.Normal)
 62         secondBtn.backgroundColor=UIColor.redColor()
 63         secondBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
 64         secondBtn.titleLabel?.font=UIFont.systemFontOfSize(15/375.0*SW)
 65         secondBtn.frame=CGRectMake(SW/2-20/375.0*SW, 100/375.0*SW, SW/2-20/375.0*SW, 50/375.0*SW)
 66         secondBtn.chooseClassify { (button) in
 67             self.secondChoose(button)
 68         }
 69         view1.addSubview(secondBtn)
 70     }
 71 //隐藏展示在中间的Alert
 72     func hideCenterAlert() {
 73         self.backGroundView.alpha=0
 74         self.removeFromSuperview()
 75     }
 76 //显示展示在下面的Alert
 77     func showZLAlert(message message :String, first :String, block1 :FirstChoose, second :String, block2 :SecondChoose) {
 78 
 79         firstChoose=block1
 80         secondChoose=block2
 81 
 82         tap = UITapGestureRecognizer(target: self,action: #selector(hideAlert(tap:)))
 83         self.addGestureRecognizer(tap)
 84         self.addSubview(backGroundView)
 85 
 86         view1 = UIView(frame: CGRectMake( 10/375.0*SW, SH, SW-20/375.0*SW, 90/375.0*SW))
 87         view1.backgroundColor=UIColor.whiteColor()
 88         view1.layer.masksToBounds=true
 89         view1.layer.cornerRadius=10/375.0*SW
 90         self.addSubview(view1)
 91 
 92         view2 = UIView(frame: CGRectMake( 10/375.0*SW, SH+100/375.0*SW, SW-20/375.0*SW, 40/375.0*SW))
 93         view2.backgroundColor=UIColor.whiteColor()
 94         view2.layer.masksToBounds=true
 95         view2.layer.cornerRadius=10/375.0*SW
 96         self.addSubview(view2)
 97 
 98         let messageLab = UILabel(frame: CGRectMake( 0, 0, SW-20/375.0*SW, 50/375.0*SW))
 99         messageLab.text=message
100         messageLab.font=UIFont.systemFontOfSize(17/375.0*SW)
101         messageLab.textColor=UIColor.lightGrayColor()
102         messageLab.textAlignment=NSTextAlignment.Center
103         messageLab.layer.borderColor=RGB(230, g: 230, b: 230, a: 1).CGColor
104         messageLab.layer.borderWidth=0.5
105         view1.addSubview(messageLab)
106 
107         let firstBtn = ZLButton(type: UIButtonType.System)
108         firstBtn.setTitle(first, forState: UIControlState.Normal)
109         firstBtn.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
110         firstBtn.frame=CGRectMake(0, 50/375.0*SW, SW-20/375.0*SW, 40/375.0*SW)
111         firstBtn.titleLabel?.font=UIFont.systemFontOfSize(15/375.0*SW)
112         firstBtn.chooseClassify { (button) in
113             self.firstChoose(button)
114         }
115         view1.addSubview(firstBtn)
116 
117         let secondBtn = ZLButton(type: UIButtonType.System)
118         secondBtn.setTitle(second, forState: UIControlState.Normal)
119         secondBtn.setTitleColor(UIColor.greenColor(), forState: UIControlState.Normal)
120         secondBtn.titleLabel?.font=UIFont.systemFontOfSize(15/375.0*SW)
121         secondBtn.frame=view2.bounds
122         secondBtn.chooseClassify { (button) in
123             self.secondChoose(button)
124         }
125         view2.addSubview(secondBtn)
126 
127         UIView.animateWithDuration(0.3, animations: { 
128             self.view1.frame=CGRectMake( 10/375.0*SW, SH-170/375.0*SW, SW-20/375.0*SW, 90/375.0*SW)
129             self.view2.frame=CGRectMake( 10/375.0*SW, SH-70/375.0*SW, SW-20/375.0*SW, 40/375.0*SW)
130         }) { (finished:Bool) in
131                 UIView.animateWithDuration(0.1, animations: { 
132                     self.view1.frame=CGRectMake( 10/375.0*SW, SH-150/375.0*SW, SW-20/375.0*SW, 90/375.0*SW)
133                     self.view2.frame=CGRectMake( 10/375.0*SW, SH-50/375.0*SW, SW-20/375.0*SW, 40/375.0*SW)
134                 })
135         }
136     }
137 
138     func hideAlert(tap tap :UITapGestureRecognizer) {
139         hideView()
140     }
141 //隐藏展示在下面的Alert
142     func hideView() {
143         UIView.animateWithDuration(0.3, animations: { 
144             self.view1.frame=CGRectMake( 10/375.0*SW, SH, SW-20/375.0*SW, 90/375.0*SW)
145             self.view2.frame=CGRectMake( 10/375.0*SW, SH+100/375.0*SW, SW-20/375.0*SW, 40/375.0*SW)
146             }) { (finished:Bool) in
147                 self.backGroundView.alpha=0
148                 self.removeFromSuperview()
149         }
150     }
151 
152     required init?(coder aDecoder: NSCoder) {
153         fatalError("init(coder:) has not been implemented")
154     }
155 }

 

转载于:https://www.cnblogs.com/nanfanggulang/p/5852981.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值