★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-unluttcs-ky.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
目錄:[Swift]通天遁地Swiftios
本文將演示第三方類庫對顏色類的擴展。git
首先確保在項目中已經安裝了所需的第三方庫。github
點擊【Podfile】,查看安裝配置文件。swift
1 platform :ios, '12.0' 2 use_frameworks! 3 4 target 'DemoApp' do 5 source 'https://github.com/CocoaPods/Specs.git' 6 pod 'DynamicColor' 7 end
根據配置文件中的相關配置,安裝第三方庫。數組
而後點擊打開【DemoApp.xcworkspace】項目文件。微信
在項目導航區,打開視圖控制器的代碼文件【ViewController.swift】ide
如今開始編寫代碼,得到某個顏色的亮色、暗色、灰階、反色、混合色等。佈局
1 import UIKit 2 //在當前的類文件中,引入已經安裝的第三方類庫 3 import DynamicColor 4 5 //添加集合視圖數據源協議UICollectionViewDataSource和代理協議UICollectionViewDelegate 6 class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { 7 8 //初始化一個字符串常量,做爲集合視圖單元格的複用標識。 9 private let ColorCellIdentifier = "ColorCell" 10 11 //繼續添加一個集合視圖變量,做爲當前類的屬性。 12 //將使用此集合視圖,展現顏色的各類變化 13 var colorCollectionView: UICollectionView! 14 15 //初始化一個顏色數組,做爲集合視圖的數據源 16 private lazy var colors: [(String, UIColor)] = { 17 let mainColor = UIColor(hexString: "#c0392b") 18 19 //返回一個顏色數組 20 return [ 21 //原色 22 ("Original", mainColor), 23 //亮色 24 ("Lighter", mainColor.lighter()), 25 //暗色 26 ("Darkered", mainColor.darkened()), 27 //飽和度加強 28 ("Saturated", mainColor.saturated()), 29 //飽和度減弱 30 ("Desaturated", mainColor.desaturated()), 31 //灰調 32 ("Grayscaled", mainColor.grayscaled()), 33 //調整色相 34 ("Adjusted", mainColor.adjustedHue(amount: 45)), 35 //互補色 36 ("Complemented", mainColor.complemented()), 37 //反色 38 ("Inverted", mainColor.inverted()), 39 //藍色 40 ("Mix Blue", mainColor.mixed(withColor: .blue)), 41 //綠色 42 ("Mix Green", mainColor.mixed(withColor: .green)), 43 //黃色 44 ("Mix Yellow", mainColor.mixed(withColor: .yellow)), 45 //混合色 46 ("Tinted", mainColor.tinted()), 47 //陰影色 48 ("Shaded", mainColor.shaded()) 49 ] 50 }() 51 52 //初始化一個數組,用來存儲漸變顏色 53 private lazy var gradients: [(String, UIColor)] = { 54 //返回一個由紅黃藍三色組成的漸變顏色 55 return [UIColor.red, 56 UIColor.yellow , 57 UIColor.blue].gradient.colorPalette(amount: 15).map { ($0.toHexString(), $0) } 58 }() 59 60 override func viewDidLoad() 61 { 62 super.viewDidLoad() 63 64 //初始化集合視圖的流動佈局對象 65 let layout = UICollectionViewFlowLayout() 66 //設置佈局對象的底部區域的參數尺寸 67 layout.footerReferenceSize = CGSize(width: 320, height: 80) 68 69 //初始化一個集合視圖對象,並設置該對象的顯示區域和佈局屬性 70 colorCollectionView = UICollectionView(frame: CGRect(x: 0, y: 20, width: 320, height: 548), collectionViewLayout: layout) 71 //設置集合視圖的數據源,爲當前的視圖控制器對象 72 colorCollectionView.dataSource = self 73 //給集合視圖進行註冊,並設置單元格的複用標識 74 colorCollectionView.register(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: ColorCellIdentifier) 75 76 //將集合視圖添加到根視圖 77 self.view.addSubview(colorCollectionView) 78 //並從新加載集合視圖的數據 79 colorCollectionView.reloadData() 80 } 81 82 //添加一個方法,用來設置集合視圖的段落爲2 83 func numberOfSections(in collectionView: UICollectionView) -> Int 84 { 85 //第一個段落用來顯示各類擴展色 86 //第一個段落用來顯示漸變色 87 return 2 88 } 89 90 //添加一個方法,根據段落的不一樣,返回不一樣的數據源 91 func collection(inSection section: Int) -> [(String, UIColor)] 92 { 93 return section == 0 ? colors : gradients 94 } 95 96 //添加一個方法,設置段落中單元格的數量 97 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 98 { 99 //根據段落的不一樣,返回不一樣的單元格數量 100 return collection(inSection: section).count 101 } 102 103 //添加一個方法,用來初始化或複用集合視圖的單元格 104 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 105 { 106 //根據複用標識,從集合視圖中獲取能夠複用的單元格 107 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ColorCellIdentifier, for: indexPath) 108 //根據指定的段落和行數得到對應的標題和顏色 109 let (title, color) = collection(inSection: indexPath.section)[indexPath.row] 110 111 //得到在單元格中,表示值爲1的單元格對象 112 var label = cell.viewWithTag(1) as? UILabel 113 //若是沒有該標籤對象, 114 if(label == nil) 115 { 116 //則初始化一個新的標籤對象,並設置其顯示區域 117 label = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) 118 //設置標籤對象的字體屬性 119 label?.font = UIFont(name: "Arial", size: 10) 120 //設置標籤對象的標識值爲1 121 label?.tag = 1 122 //設置標籤對象的文字對齊方式爲居中對齊, 123 label?.textAlignment = .center 124 //並將標籤對象添加到單元格中。 125 cell.addSubview(label!) 126 } 127 128 //設置標籤對象的文字內容 129 label?.text = title 130 //設置單元格的背景顏色,爲數據源中的顏色 131 cell.backgroundColor = color 132 133 //返回設置好的單元格 134 return cell 135 } 136 }
模擬器的上方顯示了由原始色擴展出的各類顏色,而在下方的區域則顯示了一組漸變顏色。post