iOS UIColorPickerViewController

2022-07-31  本文已影响0人  iOS虞

iOS 14 新增了一个UIColorPickerViewController控制器用来选择颜色

Simulator Screen Shot - iPhone SE (3rd generation) - 2022-07-31 at 11.21.44.png Simulator Screen Shot - iPhone SE (3rd generation) - 2022-07-31 at 11.21.50.png Simulator Screen Shot - iPhone SE (3rd generation) - 2022-07-31 at 11.21.54.png

这样我们就可以愉快的在App内改变想要的颜色了,实现代码也超级的简单

import UIKit

class ColorViewController: UIColorPickerViewController {

    var selectColorBlock: ((UIColor) -> Void)?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.delegate = self
    }
}

extension ColorViewController: UIColorPickerViewControllerDelegate {
    
    func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {
        
        print(viewController.selectedColor)
    }
    
    func colorPickerViewController(_ viewController: UIColorPickerViewController, didSelect color: UIColor, continuously: Bool) {
        selectColorBlock?(color)
    }
    
    func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
        
        print(viewController.selectedColor)
    }
}

let colorVC = ColorViewController()
        colorVC.selectColorBlock = { [weak self] cor in
            self?.view.backgroundColor = cor
        }
        self.present(colorVC, animated: true)
上一篇下一篇

猜你喜欢

热点阅读