UIColorPickerViewController提供了一個選擇顏色的標(biāo)準(zhǔn)接口。

UIColorPickerViewController官網(wǎng)截圖.png
一、第一步就要先遵循UIColorPickerViewController代理

UIColorPickerViewController代理.png
@interface ViewController ()<UIColorPickerViewControllerDelegate>
@end
二、第二步添加UIColorPickerViewController
//使用touchesBegan觸摸方法點擊彈出
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UIColorPickerViewController *colourPickerVC = [[UIColorPickerViewController alloc] init];
// 遵循代理
colourPickerVC.delegate = self;
// 在顏色選擇器上設(shè)置所選顏色,并在用戶更改選擇時更新。
colourPickerVC.selectedColor = self.view.backgroundColor;
// 如果設(shè)置為“NO”,用戶只能選擇完全不透明的顏色。
colourPickerVC.supportsAlpha = NO;
[self presentViewController:colourPickerVC animated:YES completion:nil];
}
//此時點擊效果如下圖:

手機(jī)運(yùn)行效果圖截屏.jpg
三、第三步添加代理方法
#pragma mark - 見下面截圖
#pragma mark - Informs the delegate when the user selects a color.當(dāng)用戶選擇顏色時通知代理。
- (void)colorPickerViewControllerDidSelectColor:(UIColorPickerViewController *)viewController{
UIColor *cpSelectedColour = viewController.selectedColor;
self.view.backgroundColor = cpSelectedColour;
}
/*
In presentations (except popovers) the color picker shows a close button. If the close button is tapped,
在演示文稿中(除了彈出窗口),顏色選擇器顯示一個關(guān)閉按鈕。如果按下關(guān)閉按鈕,
*/
//the view controller is dismissed and `colorPickerViewControllerDidFinish:` is called. Can be used to
//animate alongside the dismissal.
#pragma mark -Informs the delegate that the user dismissed the color picker. 開始解除的視圖控制器。
- (void)colorPickerViewControllerDidFinish:(UIColorPickerViewController *)viewController{
UIColor *cpSelectedColour = viewController.selectedColor;
self.view.backgroundColor = cpSelectedColour;
}
//#pragma mark -Informs the delegate when a user selects a color, indicating whether the update is part of a continuous user interaction.
//#pragma mark -當(dāng)用戶選擇顏色時通知代理,指示更新是否是連續(xù)用戶交互的一部分。
//- (void)colorPickerViewController:(UIColorPickerViewController *)viewController
// didSelectColor:(UIColor *)color
// continuously:(BOOL)continuously{
// 這個代理方法不用寫,目前業(yè)務(wù)用不到,知道就行
//}

colorPickerViewControllerDidSelectColor官網(wǎng)截圖.jpg
https://github.com/ZongAng123/UIColorPickerViewController.git