在iOS库中,苹果提供的一系列内置控件中最显眼缺失的一个就是颜色选择器。许多开源组件应运而生以填补这一空白。这个组件是为了[ToonThat][https://itunes.apple.com/us/app/toonthat/id584478951?ls=1&mt=8]应用程序而创建的,因为我们没有在其他工具中找到类似的灵活性和功能集。
将源文件夹中的文件复制到您的项目中。colorPicker.bundle文件包含图形资源。
在您想要调用颜色选择器的视图控制器中,首先实现代理的方法
@implementation MyViewController <NEOColorPickerViewControllerDelegate>
假设您希望在按钮被点击后启动视图控制器,设置并启动一个NEOColorPickerViewController实例
NEOColorPickerViewController *controller = [[NEOColorPickerViewController alloc] init];
controller.delegate = self;
controller.selectedColor = <some initial color reference>;
controller.title = @"My dialog title";
UINavigationController* navVC = [[UINavigationController alloc] initWithRootViewController:controller];
[self presentViewController:navVC animated:YES completion:nil];
最后处理颜色选择器代理回调,在颜色被选择或取消时
- (void) colorPickerViewController:(NEOColorPickerBaseViewController *)controller didSelectColor:(UIColor *)color {
// Do something with the color.
self.view.backgroundColor = color;
[controller dismissViewControllerAnimated:YES completion:nil];
}
- (void) colorPickerViewControllerDidCancel:(NEOColorPickerBaseViewController *)controller {
[controller dismissViewControllerAnimated:YES completion:nil];
}