• [Swift通天遁地]五、高级扩展-(4)快速生成Invert、Mix、Tint、Shade颜色及调整饱和度阶


    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/10235607.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    目录:[Swift]通天遁地Swift

    本文将演示第三方类库对颜色类的扩展。

    首先确保在项目中已经安装了所需的第三方库。

    点击【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

    根据配置文件中的相关配置,安装第三方库。

    然后点击打开【DemoApp.xcworkspace】项目文件。

    在项目导航区,打开视图控制器的代码文件【ViewController.swift】

    现在开始编写代码,获得某个颜色的亮色、暗色、灰阶、反色、混合色等。

      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( 320, height: 80)
     68         
     69         //初始化一个集合视图对象,并设置该对象的显示区域和布局属性
     70         colorCollectionView = UICollectionView(frame: CGRect(x: 0, y: 20,  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,  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 }

     模拟器的上方显示了由原始色扩展出的各种颜色,而在下方的区域则显示了一组渐变颜色。

  • 相关阅读:
    [Python] Python2 、Python3 urllib 模块对应关系
    [Python] Mac pip安装的模块包路径以及常规python路径
    git 使用详解
    版本控制工具简介
    python基础练习题(二)
    python简介,安装
    python基础练习题(一)
    python练习题之面向对象(三)
    python之input函数,if,else条件语句使用的练习题(一)
    C++ 静态变量
  • 原文地址:https://www.cnblogs.com/strengthen/p/10235607.html
Copyright © 2020-2023  润新知