美文网首页
iOS 14 新属性-UIColorPickerViewCont

iOS 14 新属性-UIColorPickerViewCont

作者: 纵昂 | 来源:发表于2023-05-28 08:25 被阅读0次

UIColorPickerViewController提供了一个选择颜色的标准接口。

UIColorPickerViewController官网截图.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;
//  在颜色选择器上设置所选颜色,并在用户更改选择时更新。
    colourPickerVC.selectedColor = self.view.backgroundColor;
//  如果设置为“NO”,用户只能选择完全不透明的颜色。
    colourPickerVC.supportsAlpha = NO;
    [self presentViewController:colourPickerVC animated:YES completion:nil];
    
    
}
//此时点击效果如下图:
手机运行效果图截屏.jpg
三、第三步添加代理方法
#pragma mark - 见下面截图
#pragma mark - Informs the delegate when the user selects a color.当用户选择颜色时通知代理。
- (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,
 在演示文稿中(除了弹出窗口),颜色选择器显示一个关闭按钮。如果按下关闭按钮,
 */

//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 -当用户选择颜色时通知代理,指示更新是否是连续用户交互的一部分。
//- (void)colorPickerViewController:(UIColorPickerViewController *)viewController
//                   didSelectColor:(UIColor *)color
//                     continuously:(BOOL)continuously{
//    这个代理方法不用写,目前业务用不到,知道就行
//}
colorPickerViewControllerDidSelectColor官网截图.jpg
https://github.com/ZongAng123/UIColorPickerViewController.git

相关文章

网友评论

      本文标题:iOS 14 新属性-UIColorPickerViewCont

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