[Swift通天遁地]五、高级扩展-(4)快速生成Invert、Mix、Tint、Shade颜色及调整饱和度阶...
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號(hào):山青詠芝(shanqingyongzhi)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:https://www.cnblogs.com/strengthen/p/10235607.html?
?如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
?原文已修改更新!強(qiáng)烈建議點(diǎn)擊原文地址閱讀!支持作者!支持原創(chuàng)!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
目錄:[Swift]通天遁地Swift
本文將演示第三方類(lèi)庫(kù)對(duì)顏色類(lèi)的擴(kuò)展。
首先確保在項(xiàng)目中已經(jīng)安裝了所需的第三方庫(kù)。
點(diǎn)擊【Podfile】,查看安裝配置文件。
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根據(jù)配置文件中的相關(guān)配置,安裝第三方庫(kù)。
然后點(diǎn)擊打開(kāi)【DemoApp.xcworkspace】項(xiàng)目文件。
在項(xiàng)目導(dǎo)航區(qū),打開(kāi)視圖控制器的代碼文件【ViewController.swift】
現(xiàn)在開(kāi)始編寫(xiě)代碼,獲得某個(gè)顏色的亮色、暗色、灰階、反色、混合色等。
1 import UIKit 2 //在當(dāng)前的類(lèi)文件中,引入已經(jīng)安裝的第三方類(lèi)庫(kù) 3 import DynamicColor 4 5 //添加集合視圖數(shù)據(jù)源協(xié)議UICollectionViewDataSource和代理協(xié)議UICollectionViewDelegate 6 class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { 7 8 //初始化一個(gè)字符串常量,作為集合視圖單元格的復(fù)用標(biāo)識(shí)。 9 private let ColorCellIdentifier = "ColorCell" 10 11 //繼續(xù)添加一個(gè)集合視圖變量,作為當(dāng)前類(lèi)的屬性。 12 //將使用此集合視圖,展示顏色的各種變化 13 var colorCollectionView: UICollectionView! 14 15 //初始化一個(gè)顏色數(shù)組,作為集合視圖的數(shù)據(jù)源 16 private lazy var colors: [(String, UIColor)] = { 17 let mainColor = UIColor(hexString: "#c0392b") 18 19 //返回一個(gè)顏色數(shù)組 20 return [ 21 //原色 22 ("Original", mainColor), 23 //亮色 24 ("Lighter", mainColor.lighter()), 25 //暗色 26 ("Darkered", mainColor.darkened()), 27 //飽和度增強(qiáng) 28 ("Saturated", mainColor.saturated()), 29 //飽和度減弱 30 ("Desaturated", mainColor.desaturated()), 31 //灰調(diào) 32 ("Grayscaled", mainColor.grayscaled()), 33 //調(diào)整色相 34 ("Adjusted", mainColor.adjustedHue(amount: 45)), 35 //互補(bǔ)色 36 ("Complemented", mainColor.complemented()), 37 //反色 38 ("Inverted", mainColor.inverted()), 39 //藍(lán)色 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 //初始化一個(gè)數(shù)組,用來(lái)存儲(chǔ)漸變顏色 53 private lazy var gradients: [(String, UIColor)] = { 54 //返回一個(gè)由紅黃藍(lán)三色組成的漸變顏色 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 //初始化集合視圖的流動(dòng)布局對(duì)象 65 let layout = UICollectionViewFlowLayout() 66 //設(shè)置布局對(duì)象的底部區(qū)域的參數(shù)尺寸 67 layout.footerReferenceSize = CGSize(width: 320, height: 80) 68 69 //初始化一個(gè)集合視圖對(duì)象,并設(shè)置該對(duì)象的顯示區(qū)域和布局屬性 70 colorCollectionView = UICollectionView(frame: CGRect(x: 0, y: 20, width: 320, height: 548), collectionViewLayout: layout) 71 //設(shè)置集合視圖的數(shù)據(jù)源,為當(dāng)前的視圖控制器對(duì)象 72 colorCollectionView.dataSource = self 73 //給集合視圖進(jìn)行注冊(cè),并設(shè)置單元格的復(fù)用標(biāo)識(shí) 74 colorCollectionView.register(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: ColorCellIdentifier) 75 76 //將集合視圖添加到根視圖 77 self.view.addSubview(colorCollectionView) 78 //并重新加載集合視圖的數(shù)據(jù) 79 colorCollectionView.reloadData() 80 } 81 82 //添加一個(gè)方法,用來(lái)設(shè)置集合視圖的段落為2 83 func numberOfSections(in collectionView: UICollectionView) -> Int 84 { 85 //第一個(gè)段落用來(lái)顯示各種擴(kuò)展色 86 //第一個(gè)段落用來(lái)顯示漸變色 87 return 2 88 } 89 90 //添加一個(gè)方法,根據(jù)段落的不同,返回不同的數(shù)據(jù)源 91 func collection(inSection section: Int) -> [(String, UIColor)] 92 { 93 return section == 0 ? colors : gradients 94 } 95 96 //添加一個(gè)方法,設(shè)置段落中單元格的數(shù)量 97 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 98 { 99 //根據(jù)段落的不同,返回不同的單元格數(shù)量 100 return collection(inSection: section).count 101 } 102 103 //添加一個(gè)方法,用來(lái)初始化或復(fù)用集合視圖的單元格 104 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 105 { 106 //根據(jù)復(fù)用標(biāo)識(shí),從集合視圖中獲取可以復(fù)用的單元格 107 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ColorCellIdentifier, for: indexPath) 108 //根據(jù)指定的段落和行數(shù)獲得對(duì)應(yīng)的標(biāo)題和顏色 109 let (title, color) = collection(inSection: indexPath.section)[indexPath.row] 110 111 //獲得在單元格中,表示值為1的單元格對(duì)象 112 var label = cell.viewWithTag(1) as? UILabel 113 //如果沒(méi)有該標(biāo)簽對(duì)象, 114 if(label == nil) 115 { 116 //則初始化一個(gè)新的標(biāo)簽對(duì)象,并設(shè)置其顯示區(qū)域 117 label = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) 118 //設(shè)置標(biāo)簽對(duì)象的字體屬性 119 label?.font = UIFont(name: "Arial", size: 10) 120 //設(shè)置標(biāo)簽對(duì)象的標(biāo)識(shí)值為1 121 label?.tag = 1 122 //設(shè)置標(biāo)簽對(duì)象的文字對(duì)齊方式為居中對(duì)齊, 123 label?.textAlignment = .center 124 //并將標(biāo)簽對(duì)象添加到單元格中。 125 cell.addSubview(label!) 126 } 127 128 //設(shè)置標(biāo)簽對(duì)象的文字內(nèi)容 129 label?.text = title 130 //設(shè)置單元格的背景顏色,為數(shù)據(jù)源中的顏色 131 cell.backgroundColor = color 132 133 //返回設(shè)置好的單元格 134 return cell 135 } 136 }?模擬器的上方顯示了由原始色擴(kuò)展出的各種顏色,而在下方的區(qū)域則顯示了一組漸變顏色。
轉(zhuǎn)載于:https://www.cnblogs.com/strengthen/p/10235607.html
總結(jié)
以上是生活随笔為你收集整理的[Swift通天遁地]五、高级扩展-(4)快速生成Invert、Mix、Tint、Shade颜色及调整饱和度阶...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 学习计划1
- 下一篇: day22-面向对象之封装