iOS 14 新屬性-UIColorPickerViewController使用

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容