美文网首页
iOS UIColorPickerViewController

iOS UIColorPickerViewController

作者: iOS虞 | 来源:发表于2022-07-31 11:36 被阅读0次

    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)
    

    相关文章

      网友评论

          本文标题:iOS UIColorPickerViewController

          本文链接:https://www.haomeiwen.com/subject/kejkwrtx.html