UIColorPickerViewController </ b> </ code> that appeared in iOS14,
It was easy to implement and very convenient, so I will post it so that it can be used by copying.
This time, we are implementing with Storyboard </ b> instead of SwiftUI.
Execution environment | version |
---|---|
Xcode | 12.0 |
Swift | 5 |
A color palette that allows you to freely select colors and adjust transparency.
When you start it, it will be a simple application that ColorPickerViewController </ code> will be displayed.
When you select a color, it will be reflected in the
backgraoundColor </ code> of the
view </ code>.
python
import UIKit
class CPSampleViewController: UIViewController {
var colorPicker = UIColorPickerViewController()
var selectedColor = UIColor.white
override func viewDidLoad() {
super.viewDidLoad()
colorPicker.delegate = self
view.backgroundColor = selectedColor
appearColorPicker()
}
func appearColorPicker() {
colorPicker.supportsAlpha = true
colorPicker.selectedColor = selectedColor
present(colorPicker, animated: true)
}
}
extension CPSampleViewController: UIColorPickerViewControllerDelegate {
func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {
selectedColor = viewController.selectedColor
view.backgroundColor = selectedColor
}
func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
print("dismissed colorPicker")
}
}
Explain the properties of UIColorPicekrViewController </ code>
First of all
python
colorPicker.supportsAlpha = true
・ Apple official document supportsAlpha A Boolean value that enables alpha value control.
Bool value that allows control of transparency </ b> ... apparently ...
In other words
Setting the value of supportsAlpha </ code> to
false </ font> </ code> hides the ColorPicker's Opacity controller.
python
colorPicker.supportsAlpha = false
Next,
python
colorPicker.selectedColor = selectedColor
・ Apple official document selectedColor The color selected by the user.
User-selected color </ b> ... apparently ...
That's right.
You can get the selected color </ b> from this value, or determine the selected color </ b> when the UIColorPickerViewController </ code> is opened. can.
Explanation of delegate method </ b> of UIColorPicekrViewController </ code>
First,
python
func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {
}
・ Apple official document colorPickerViewControllerDidSelectColor(_:) Informs the delegate when the user selects a color.
delegate method called when the user selects a color </ b> ... apparently ...
This time, we are using this delegate to assign the value of the selected color to the global variable.
continue,
python
func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
}
・ Apple official document colorPickerViewControllerDidSelectColor(_:) Informs the delegate that the user dismissed the view controller.
Method called when user dismiss UIColorPickerViewController </ b> ... apparently ...
It's very easy to implement, so I want to use it more and more! However, it can't be used unless it is compatible with iOS14, so it seems like I'm watching around there.
If you find any mistakes, please kindly correct them.
Recommended Posts