Swift-custom Alert, swift-alert

Source: Internet
Author: User

Swift-custom Alert, swift-alert

Preface

1. Because various prompt boxes are frequently used in my project, but apple does not meet product requirements, I have customized an Alert for practice.

2. You are welcome to correct the old bird.

Okay, with code.

1. Use a Custom button in Alert

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. Custom 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: String, value1: String, block1: FirstChoose, value2: String, block2: SecondChoose) in the center) {27 firstChoose = block1 28 secondChoose = block2 29 30 tap = UITapGestureRecognizer (target: self, action: # select Or (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: CG RectMake (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 (val Ue1, forState: UIControlState. normal) 52 firstBtn. setTitleColor (UIColor. blackColor (), forState: UIControlState. normal) 53 firstBtn. frame = CGRectMake (0, 100/375. 0 x 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 s EcondBtn. titleLabel ?. Font = UIFont. systemFontOfSize (15/375. 0 * SW) 65 secondBtn. frame = CGRectMake (SW/2-20/375. 0 * SW, 100/375. 0 x 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 // hide the Alert 72 func hideCenterAlert () {73 self. backGroundView. alpha = 0 74 self. removeFromSuperview () 75} 76 // The Alert 77 func showZLAlert (me Ssage message: String, first: String, block1: FirstChoose, second: String, block2: SecondChoose) {78 79 firstChoose = block1 80 secondChoose = block2 81 82 tap = require (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.back GroundColor = UIColor. whiteColor () 88 view1.layer. masksToBounds = true 89 view1.layer. cornerRadius = 10/375. 0 * SW 90 self. addSubview (view1) 9192 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 fi RstBtn = ZLButton (type: UIButtonType. 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 x SW) 111 firstBtn. titleLabel ?. Font = UIFont. systemFontOfSize (15/375. 0 x 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 x 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 func hideAlert (tap: UITapGestureRecognizer) {138 hideView () 139} 140 // hide the following 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}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.