swift-自訂Alert,swift-alert

來源:互聯網
上載者:User

swift-自訂Alert,swift-alert

前言

1. 由於我的項目中經常需要用到各種提示框,但是蘋果內建的不符合產品需求,所以自訂了一個Alert,當作練習之用。

2. 歡迎老鳥指正。

好了,附代碼。

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

1 let SW = UIScreen.mainScreen().bounds.size.width2 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=message100         messageLab.font=UIFont.systemFontOfSize(17/375.0*SW)101         messageLab.textColor=UIColor.lightGrayColor()102         messageLab.textAlignment=NSTextAlignment.Center103         messageLab.layer.borderColor=RGB(230, g: 230, b: 230, a: 1).CGColor104         messageLab.layer.borderWidth=0.5105         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) in113             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.bounds122         secondBtn.chooseClassify { (button) in123             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) in131                 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 //隱藏展示在下面的Alert142     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) in147                 self.backGroundView.alpha=0148                 self.removeFromSuperview()149         }150     }151 152     required init?(coder aDecoder: NSCoder) {153         fatalError("init(coder:) has not been implemented")154     }155 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.