1. 程式人生 > >[Swift通天遁地]一、超級工具-(15)使用SCLAlertView製作強大的Alert警告視窗和Input編輯視窗

[Swift通天遁地]一、超級工具-(15)使用SCLAlertView製作強大的Alert警告視窗和Input編輯視窗

本文將演示如何知錯一款可以採集使用者資料的提示視窗。

首先確保在專案中已經安裝了所需的第三方庫。

1 platform :ios, '12.0'
2 use_frameworks!
3 
4 target 'DemoApp' do
5     source 'https://github.com/CocoaPods/Specs.git'
6     pod 'SCLAlertView'
7 end

點選【Podfile】,檢視安裝配置檔案。

根據配置檔案中的相關配置,安裝第三方庫

然後點選開啟【DemoApp.xcworkspace】專案檔案。

在專案導航區,開啟檢視控制器的程式碼檔案【ViewController.swift】

  1 import UIKit
  2 //在當前的專案檔案中,引入已經安裝的第三方類庫
  3 import SCLAlertView
  4 
  5 class ViewController: UIViewController {
  6 
  7     override func viewDidLoad() {
  8         super.viewDidLoad()
  9         // Do any additional setup after loading the view, typically from a nib.
 10         
 11
//初始化一個按鈕,當用戶點選該按鈕時,彈出一個警告視窗 12 let popup = UIButton(frame: CGRect(x: 0, y: 0, width: 320, height: 40)) 13 //將按鈕控制元件放置在根檢視的中心位置 14 popup.center = self.view.center 15 //設定按鈕控制元件的背景顏色為橙色 16 popup.backgroundColor = UIColor.orange 17 //設定按鈕控制元件在正常狀態下的標題文字
18 popup.setTitle("Popup", for: .normal) 19 //給按鈕控制元件繫結點選事件 20 popup.addTarget(self, action: #selector(ViewController.popUp), for: .touchUpInside) 21 22 //設定根檢視的背景顏色為橙色 23 self.view.backgroundColor = UIColor.orange 24 //並將按鈕新增到根檢視中 25 self.view.addSubview(popup) 26 } 27 28 //新增一個方法,用來響應按鈕的點選事件 29 func popUp() 30 { 31 //1.初始化一個資訊型別的彈出視窗, 32 //並設定彈出視窗的標題和子標題。 33 SCLAlertView().showInfo("Important info", subTitle: "You are great") 34 35 //2.對程式碼進行一些修改 36 //初始化一個成功型別的彈出視窗,並設定彈出視窗的標題和子標題。 37 let alertViewResponder: SCLAlertViewResponder = SCLAlertView().showSuccess("Hello World", 38 subTitle: "This is a more descriptive text.") 39 //可以通過彈出視窗的設定標題方法,重新設定視窗的標題文字。 40 alertViewResponder.setTitle("New Title") 41 //可以通過彈出視窗的設定子標題方法,重新設定視窗的子標題文字。 42 alertViewResponder.setSubTitle("New description")*/ 43 44 //3.對程式碼進行一些修改 45 //彈出視窗包含多種樣式。 46 //錯誤型別的彈出視窗,並設定彈出視窗的標題和子標題。 47 SCLAlertView().showError("Hello Error", subTitle: "This is a more descriptive error text.") 48 //通知型別的彈出視窗,並設定彈出視窗的標題和子標題。 49 SCLAlertView().showNotice("Hello Notice", subTitle: "This is a more descriptive notice text.") 50 //警告型別的彈出視窗,並設定彈出視窗的標題和子標題。 51 SCLAlertView().showWarning("Hello Warning", subTitle: "This is a more descriptive warning text.") 52 //資訊型別的彈出視窗,並設定彈出視窗的標題和子標題。 53 SCLAlertView().showInfo("Hello Info", subTitle: "This is a more descriptive info text.") 54 //編輯型別的彈出視窗,並設定彈出視窗的標題和子標題。 55 SCLAlertView().showEdit("Hello Edit", subTitle: "This is a more descriptive info text.") 56 57 58 //4.對程式碼進行一些修改 59 //對彈出視窗的視覺樣式進行了修改 60 SCLAlertView().showTitle( 61 "Congratulations",//標題 62 subTitle: "Operation successfully completed.",//子標題 63 duration: 2.0,//持續時長 64 completeText: "Done",//完成提示文字 65 style: .success,//主題樣式 66 colorStyle: 0xA429FF,//視窗顏色 67 colorTextButton: 0xFFFFFF//按鈕文字的顏色 68 ) 69 70 //5.對程式碼進行一些修改 71 //也可以初始化一個外觀樣式類的物件,從而進行視窗樣式的設定 72 let appearance = SCLAlertView.SCLAppearance( 73 //標題字型 74 kTitleFont: UIFont(name: "HelveticaNeue", size: 20)!, 75 //內容文字字型 76 kTextFont: UIFont(name: "HelveticaNeue", size: 14)!, 77 //按鈕字型 78 kButtonFont: UIFont(name: "HelveticaNeue-Bold", size: 14)!, 79 //設定不允許關閉按鈕的顯示 80 showCloseButton: false 81 ) 82 83 //根據上文的外觀樣式物件,初始化一個指定外觀樣式的彈出視窗。 84 let alert = SCLAlertView(appearance: appearance) 85 //呼叫視窗顯示的通知方法,建立一個通知型別的彈出視窗。 86 alert.showNotice("Hello Notice", subTitle: "This is a more descriptive notice text.") 87 88 //6.對程式碼進行一些修改 89 //初始化一個警告視窗 90 let alertView = SCLAlertView() 91 //在視窗中新增第一個按鈕控制元件,並給按鈕繫結點選事件 92 alertView.addButton("First Button", target:self, selector:Selector("firstButton")) 93 //在視窗中新增第二個按鈕控制元件,並給按鈕新增一個閉包語句,以響應按鈕的點選事件 94 alertView.addButton("Second Button") 95 { 96 print("Second button tapped") 97 } 98 //呼叫視窗的顯示成功方法,建立一個成功型別的彈出視窗 99 alertView.showSuccess("Button View", subTitle: "This alert view has buttons") 100 101 //7.對程式碼進行一些修改,建立一個自動隱藏的彈出視窗 102 //初始化一個外觀樣式常量 103 let appearance = SCLAlertView.SCLAppearance( 104 //並設定在視窗中不顯示關閉按鈕 105 showCloseButton: false 106 ) 107 //根據外觀樣式,建立一個彈出視窗物件 108 let alertView = SCLAlertView(appearance: appearance) 109 //呼叫視窗的顯示警告方法,開啟一個警告型別的彈出視窗,並設定在顯示三秒之後自動關閉彈出視窗 110 alertView.showWarning("No button", 111 subTitle: "Just wait for 3 seconds and I will disappear", 112 duration: 3) 113 //初始化一個外觀樣式常量 114 let appearance = SCLAlertView.SCLAppearance( 115 //設定在視窗中顯示圓形圖示 116 showCircularIcon: true 117 ) 118 //根據外觀樣式,建立一個彈出視窗物件 119 let alertView = SCLAlertView(appearance: appearance) 120 //從專案中讀取一張圖片素材 121 let alertViewIcon = UIImage(named: "Hearts") 122 //呼叫視窗的顯示資訊方法,開啟一個資訊型別的彈出視窗,並設定視窗頂部的圓形圖示 123 alertView.showInfo("Custom icon", 124 subTitle: "This is a nice alert with a custom icon you choose", 125 circleIconImage: alertViewIcon) 126 127 //8.對程式碼進行一些修改,建立一個文字輸入框的彈出視窗 128 //初始化一個新的彈出視窗物件 129 let alert = SCLAlertView() 130 //往彈出視窗中,新增一個文字框,並設定文字框的佔位文字 131 let txt = alert.addTextField("Enter your name") 132 //往彈出視窗中新增一個按鈕,後跟一個閉包語句。 133 alert.addButton("Show Name") { 134 //當點選該按鈕時,在控制檯輸出使用者在文字框中輸入的內容。 135 print("Text value: \(txt.text)") 136 } 137 //呼叫視窗的編輯方法,開啟一個編輯型別的彈出視窗 138 alert.showEdit("Edit View", subTitle: "This alert view shows a text box") 139 140 //9.對程式碼進行一些修改,建立一個包含自定義檢視的彈出視窗 141 //初始化一個新的彈出視窗物件 142 let alert = SCLAlertView() 143 144 //建立一個指定顯示區域的普通檢視 145 let subview = UIView(frame: CGRect(x: 0, y: 0, width: 216, height: 70)) 146 //然後根據檢視的寬度,計算即將新增的文字框的水平位置 147 let x = (subview.frame.width - 180) / 2 148 149 //初始化一個文字框,並將文字框放置在檢視的中心位置 150 let textfield1 = UITextField(frame: CGRect(x: x, y: 10, width: 180, height: 25)) 151 //設定文字框邊緣的顏色 152 textfield1.layer.borderColor = UIColor.green.cgColor 153 //設定文字框邊緣的寬度 154 textfield1.layer.borderWidth = 1.5 155 //設定文字框的圓角半徑 156 textfield1.layer.cornerRadius = 5 157 //設定文字框的佔位文字 158 textfield1.placeholder = "Username" 159 //設定文字框的文字對齊方式 160 textfield1.textAlignment = NSTextAlignment.center 161 //將文字框新增到檢視中 162 subview.addSubview(textfield1) 163 164 //使用相同的方式建立第二個文字框 165 let textfield2 = UITextField(frame: CGRect(x: x, y: textfield1.frame.maxY, width: 180, height: 25)) 166 //設定文字框是否為密文的顯示方式 167 textfield2.isSecureTextEntry = true 168 //設定文字框邊緣的顏色 169 textfield2.layer.borderColor = UIColor.blue.cgColor 170 //設定文字框邊緣的寬度 171 textfield2.layer.borderWidth = 1.5 172 //設定文字框的圓角半徑 173 textfield2.layer.cornerRadius = 5 174 //設定文字框的佔位文字 175 textfield2.placeholder = "Password" 176 //設定文字框的文字對齊方式 177 textfield2.textAlignment = NSTextAlignment.center 178 //將文字框新增到檢視中 179 subview.addSubview(textfield2) 180 181 //設定彈出視窗的自定義子檢視屬性 182 alert.customSubview = subview 183 //給彈出視窗設定一個互動按鈕 184 alert.addButton("Login") 185 { 186 //並設定按鈕被點選後的動作 187 print("Logged in") 188 } 189 190 //新增一個互動按鈕 191 alert.addButton("Duration Button", //文字內容 192 backgroundColor: UIColor.brown, //背景顏色 193 textColor: UIColor.yellow, //文字顏色 194 showDurationStatus: true)//按鈕的計時狀態屬性為真,時間到後自動關閉視窗 195 { 196 print("Duration Button tapped") 197 } 198 199 //呼叫視窗的顯示資訊方法,開啟一個資訊型別的彈出視窗 200 alert.showInfo("Login", subTitle: "", duration: 5) 201 } 202 203 override func didReceiveMemoryWarning() { 204 super.didReceiveMemoryWarning() 205 // Dispose of any resources that can be recreated. 206 } 207 }