★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-vbehdzxs-kz.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
目錄:[Swift]通天遁地Swiftios
本文將演示另外一款強大的第三方擴展類庫。git
首先確保在項目中已經安裝了所需的第三方庫。github
點擊【Podfile】,查看安裝配置文件。正則表達式
1 platform :ios, '12.0' 2 use_frameworks! 3 4 target 'DemoApp' do 5 source 'https://github.com/CocoaPods/Specs.git' 6 pod 'Alexandria' 7 pod 'Alexandria/Core' 8 pod 'Alexandria/StoreKit' 9 pod 'Alexandria/ImageEffects' 10 end
根據配置文件中的相關配置,安裝第三方庫。swift
而後點擊打開【DemoApp.xcworkspace】項目文件。數組
在項目導航區,打開視圖控制器的代碼文件【ViewController.swift】微信
1 import UIKit 2 //在當前的類文件中,引入已經安裝的第三方類庫 3 import Alexandria 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 IntExample() 12 //浮點類型的擴展 13 floatExamples() 14 //字符串類型的擴展 15 stringExample() 16 //圖像類型的擴展方法 17 uiimageExample() 18 //顏色類型的擴展方法 19 uicolorExample() 20 //對數組類型的擴展 21 arrayExamples() 22 //對字典類型的擴展 23 dictionaryExample() 24 //對點類型的擴展 25 CGPointExample() 26 } 27 28 //整形類型的擴展 29 func IntExample() 30 { 31 //奇數 32 print("2.isOdd:\(2.isOdd)") 33 //偶數 34 print("2.isEven:\(2.isEven)") 35 //正數 36 print("2.isPositive:\(2.isPositive)") 37 //負數 38 print("2.isNegative:\(2.isNegative)") 39 40 //經過正數快速建立一個指定次數的循環語句 41 3.repeat({_ in 42 print("Repeat in a block") 43 }) 44 45 //經過整形的類方法,快速得到指定區間的隨機正數 46 print("Int.random(min: 1, max: 10):\(Int.random(min: 1, max: 10))") 47 print("Int.random(min: 1, max: 10):\(Int.random(min: 1, max: 10))") 48 } 49 50 //浮點類型的擴展 51 func floatExamples() 52 { 53 //快速得到指定區間的隨機單精度浮點數 54 print("CGFloat.random(min:1, max:2):\(CGFloat.random(min:1, max:2))") 55 print("CGFloat.random(min:1, max:2):\(CGFloat.random(min:1, max:2))") 56 print("CGFloat.random(min:1, max:2):\(CGFloat.random(min:1, max:2))") 57 58 //快速得到指定區間的隨機浮點數 59 print("Double.random(min: 1, max: 2):\(Double.random(min: 1, max: 2))") 60 print("Float.random(min: 1, max: 2):\(Float.random(min: 1, max: 2))") 61 62 //初始化一個浮點類型的常量 63 let floatNum: CGFloat = 12.345678 64 //得到一個整數 65 print("floatNum.rounded():\(floatNum.rounded())") 66 //指定小數點的位置 67 print("floatNum.rounded(places: 2):\(floatNum.rounded(places: 2))") 68 print("floatNum.rounded(places: 3):\(floatNum.rounded(places: 3))") 69 70 //設置小數點的位置時,向最近或更大的方向進行舍入 71 print("(5.2).rounded(.toNearestOrEven):\((5.2).rounded(.toNearestOrEven))") 72 print("(5.5).rounded(.toNearestOrEven):\((5.5).rounded(.toNearestOrEven))") 73 print("(4.2).rounded(.toNearestOrEven):\((4.2).rounded(.toNearestOrEven))") 74 75 //設置小數點的位置時,向最近或遠離0的方向進行舍入 76 print("(5.2).rounded(.toNearestOrAwayFromZero):\((5.2).rounded(.toNearestOrAwayFromZero))") 77 print("(5.5).rounded(.toNearestOrAwayFromZero):\((5.5).rounded(.toNearestOrAwayFromZero))") 78 print("(-5.2).rounded(.toNearestOrAwayFromZero):\((-5.2).rounded(.toNearestOrAwayFromZero))") 79 print("(-5.5).rounded(.toNearestOrAwayFromZero):\((-5.5).rounded(.toNearestOrAwayFromZero))") 80 81 //設置小數點的位置時,向較大的方向進行舍入 82 print("(5.2).rounded(.up):\((5.2).rounded(.up))") 83 print("(5.5).rounded(.up):\((5.5).rounded(.up))") 84 print("(-5.2).rounded(.up):\((-5.2).rounded(.up))") 85 print("(-5.5).rounded(.up):\((-5.5).rounded(.up))") 86 87 //設置小數點的位置時,向遠離0的方向進行舍入 88 print("(5.2).rounded(.awayFromZero):\((5.2).rounded(.awayFromZero))") 89 print("(5.5).rounded(.awayFromZero):\((5.5).rounded(.awayFromZero))") 90 print("(-5.2).rounded(.awayFromZero):\((-5.2).rounded(.awayFromZero))") 91 print("(-5.5).rounded(.awayFromZero):\((-5.5).rounded(.awayFromZero))") 92 } 93 94 //字符串類型的擴展 95 func stringExample() 96 { 97 //駝峯命名格式 98 print("os version".camelCased) 99 //字符串內容是否爲數字 100 print("123".isNumeric) 101 //字符串內容是否爲數字 102 print("123abc".isNumeric) 103 //使用正則表達式,將字符串中的內容替換爲星號 104 print("hello".regex("[aeiou]", "*")) 105 //使用正則表達式,將字符串的兩側添加一個<> 106 print("hello".regex("([aeiou])", "<$1>")) 107 //將字符串截取至指定的位置 108 print("hello there".truncated(to: 5)) 109 //將字符串截取至指定的位置,截取的內容被替換爲省略號 110 print("hello there".truncated(to: 5, trailing: "...")) 111 } 112 113 //圖像類型的擴展方法 114 func uiimageExample() 115 { 116 //從項目中讀取一張圖片資源 117 let image = UIImage(named: "yammi_star") 118 119 //應用高亮效果 120 let newImage = image?.applyLightEffect() 121 //超級亮光效果 122 let newImage = image?.applyExtraLightEffect() 123 //變暗效果 124 let newImage = image?.applyDarkEffect() 125 //給圖片應用着色效果,並設置顏色爲紫色 126 let newImage = image?.applyTintEffect(withColor:UIColor.purple) 127 //給圖片應用模糊效果,並設置模糊的半徑、着色、飽和度比率等參數 128 let newImage = image?.applyBlur(withRadius: 4, tintColor: nil, saturationDeltaFactor: 1.5) 129 130 //着色效果,設置顏色爲洋紅色 131 let newImage = image?.tinted(UIColor.magenta) 132 //將圖片快速縮放到指定的尺寸 133 let newImage = image?.resized(width: 100, height: 100) 134 //將圖片快速縮放到指定的尺寸,同時不保持寬度的比例 135 let newImage = image?.resized(width: 200, height: 100, maintainAspectRatio: false) 136 //將圖片縮放到1.5倍的大小 137 let newImage = image?.scaled(by: 1.5) 138 139 //將圖片縮放到1.5倍的大小,並設置圖片的朝向爲向下,共有八種方向能夠選擇 140 let newImage = image?.scaled(by: 1.5, withOrientation: UIImage.Orientation.right) 141 //初始化一個圖像視圖 142 let imageView = UIImageView(image: newImage) 143 //將圖像視圖放置在根視圖的中心位置 144 imageView.center = CGPoint(x: self.view.bounds.width/2, y: self.view.bounds.height/2) 145 //將圖像視圖對象添加到根視圖 146 self.view.addSubview(imageView) 147 } 148 149 //顏色類型的擴展方法 150 func uicolorExample() 151 { 152 //初始化一個視圖對象,用來顯示自定義的顏色 153 let view = UIView(frame: CGRect(x: 20, y: 60, width: 280, height: 280)) 154 //並將視圖對象添加到根視圖 155 self.view.addSubview(view) 156 157 //初始化一個顏色對象,並設置顏色的值爲0.75 158 let color = UIColor(hex: 0x00ff00, alpha: 0.75) 159 //初始化另外一個顏色對象 160 let color2 = UIColor(hue: 0.5, //色相 161 saturation: 0.5,//飽和度 162 lightness: 0.5, //亮度 163 alpha: 1.0)//不透明度 164 //初始化一個經常使用於印刷的顏色對象 165 let color3 = UIColor(cyan: 0.25,//青色 166 magenta: 0.46,//洋紅 167 yellow: 0.39,//黃色 168 key: 0, //黑色 169 alpha: 1.0)//不透明度 170 //初始化一個基於三原色的顏色對象 171 let color4 = UIColor(red: 0, //紅 172 green: 155, //綠 173 blue: 223, //藍 174 alpha: 1.0)//不透明度 175 //建立一個顏色對象,並設置顏色對象的不透明度爲0.2 176 let color5 = color4.alpha(0.2) 177 //從一個顏色對象建立另外一個顏色對象,並提升顏色的亮度 178 let color6 = color2.lightened(by: 0.4) 179 //從一個顏色對象建立另外一個顏色對象,並下降顏色的亮度 180 let color7 = color2.darkened(by: 0.4) 181 //建立一個隨機的顏色 182 let color8 = UIColor.random 183 //輸出隨機顏色的十六進制的值 184 print("color8.hexString:\(color8.hexString)") 185 //依次輸出該顏色在四種色彩模式下的值 186 print("color8.rgba:\(color8.rgba)") 187 print("color8.hsl:\(color8.hsl)") 188 print("color8.hsb:\(color8.hsb)") 189 print("color8.cmyk:\(color8.cmyk)") 190 191 //設置視圖的背景顏色爲隨機顏色 192 view.backgroundColor = color8 193 } 194 195 //對數組類型的擴展 196 func arrayExamples() 197 { 198 //初始化一個數組 199 let array1 = [1, 2, 3, 4, 5] 200 201 //將數組的前面兩個元素,移動到數組的尾部 202 print("array1.rotated(by: 2):\(array1.rotated(by: 2))") 203 //將數組的後面兩個元素,移動到數組的頭部 204 print("array1.rotated(by: -2):\(array1.rotated(by: -2))") 205 206 //初始化一個數組 207 let array2 = [1,2,3] 208 //得到並輸出在數字3的前方的元素 209 print("array2.before(3):\(array2.before(3))") 210 //得到並輸出在數字2的前方的元素 211 print("array2.before(2):\(array2.before(2))") 212 //得到並輸出在數字1的前方的元素 213 print("array2.before(1):\(array2.before(1))") 214 215 //得到並輸出在數字後方的元素 216 print("array2.after(1):\(array2.after(1))") 217 print("array2.after(2):\(array2.after(2))") 218 print("array2.after(3):\(array2.after(3))") 219 220 //初始化一個數組 221 let array3 = [1,2,3,3,2] 222 //得到重複元素後的結果 223 print("array3.unique():\(array3.unique()))") 224 } 225 226 //對字典類型的擴展 227 func dictionaryExample() 228 { 229 //初始化一個字典對象 230 let master = ["Name":"Jerry", "Age":30, "City":"BeiJing"] as [String : Any] 231 //初始化另外一個字典對象 232 let pet = ["PetName":"Carl", "PetAge":2] as [String : Any] 233 //合併字典對象 234 let unionDict = master.union(pet) 235 //輸出合併後的字典對象 236 print("unionDict:\(unionDict)") 237 } 238 239 //對點類型的擴展 240 func CGPointExample() 241 { 242 //初始化兩個點對象 243 let firstPoint = CGPoint(x: 100, y: 100) 244 let secondPoint = CGPoint(x: 200, y: 200) 245 246 //兩點之間的距離 247 print("firstPoint.distance(to: secondPoint):\(firstPoint.distance(to: secondPoint))") 248 //兩個點對象相加 249 print("firstPoint + secondPoint:\(firstPoint + secondPoint)") 250 //兩個點對象相減 251 print("firstPoint - secondPoint:\(firstPoint - secondPoint)") 252 //兩個點對象相乘 253 print("firstPoint * secondPoint:\(firstPoint * secondPoint)") 254 //兩個點對象相乘 255 print("firstPoint / secondPoint:\(firstPoint / secondPoint)") 256 } 257 258 override func didReceiveMemoryWarning() { 259 super.didReceiveMemoryWarning() 260 // Dispose of any resources that can be recreated. 261 } 262 }