一种:iOS 颜色选择器:HRColorPicker
https://github.com/hayashi311/Color-Picker-for-iOS/
封装后对外属性:
colorInfoView(色值显示)、
colorMapView(色板控制)、
brightnessSlider(颜色深度控制条)
如果不改变UI布局,使用非常简单,几行代码就可以搞定:
#import
"HRColorPickerView.h"
@interface HRSampleColorPickerViewController () {
id < HRColorPickerViewControllerDelegate > __weak delegate;
}
@property ( nonatomic , weak ) IBOutlet HRColorPickerView *colorPickerView;
@end
@implementation HRSampleColorPickerViewController {
UIColor *_color;
}
@synthesize delegate;
- ( id )initWithCoder:( NSCoder *)coder {
self = [ super initWithCoder :coder];
if ( self ) {
}
return self ;
}
- ( void )viewDidLoad {
[ super viewDidLoad ];
self . colorPickerView . color = _color ;
[ self . colorPickerView addTarget : self
action : @selector (colorDidChange:)
forControlEvents : UIControlEventValueChanged ];
}
- ( void )viewWillDisappear:( BOOL )animated {
[ super viewWillDisappear :animated];
if ( self . delegate ) {
[ self . delegate setSelectedColor : self . color ];
}
}
- ( void )colorDidChange:( HRColorPickerView *)colorPickerView {
_color = colorPickerView. color ;
@interface HRSampleColorPickerViewController () {
id < HRColorPickerViewControllerDelegate > __weak delegate;
}
@property ( nonatomic , weak ) IBOutlet HRColorPickerView *colorPickerView;
@end
@implementation HRSampleColorPickerViewController {
UIColor *_color;
}
@synthesize delegate;
- ( id )initWithCoder:( NSCoder *)coder {
self = [ super initWithCoder :coder];
if ( self ) {
}
return self ;
}
- ( void )viewDidLoad {
[ super viewDidLoad ];
self . colorPickerView . color = _color ;
[ self . colorPickerView addTarget : self
action : @selector (colorDidChange:)
forControlEvents : UIControlEventValueChanged ];
}
- ( void )viewWillDisappear:( BOOL )animated {
[ super viewWillDisappear :animated];
if ( self . delegate ) {
[ self . delegate setSelectedColor : self . color ];
}
}
- ( void )colorDidChange:( HRColorPickerView *)colorPickerView {
_color = colorPickerView. color ;
}
一种:iOS 颜色选择器组件:MSColorPicker
(可以使用cocoaPods,pod "MSColorPicker",)
这种方法是将颜色选择页面进行整体封装,使用时跳转到封装好的页面就可以,如果想要获取选中的颜色,对外实现协议就可以获取,这种算是进行了深层的封装,所有东西在内部处理,外部只能接收。
用起来更是简单:
MSColorSelectionViewController
*colorSelectionController = [[
MSColorSelectionViewController
alloc
]
init
];
UINavigationController *navCtrl = [[ UINavigationController alloc ] initWithRootViewController :colorSelectionController];
navCtrl. modalPresentationStyle = UIModalPresentationPopover ;
navCtrl. popoverPresentationController . delegate = self ;
navCtrl. popoverPresentationController . sourceView = button;
navCtrl. popoverPresentationController . sourceRect = button. bounds ;
navCtrl. preferredContentSize = [colorSelectionController. view systemLayoutSizeFittingSize : UILayoutFittingCompressedSize ];
colorSelectionController. delegate = self ;
UINavigationController *navCtrl = [[ UINavigationController alloc ] initWithRootViewController :colorSelectionController];
navCtrl. modalPresentationStyle = UIModalPresentationPopover ;
navCtrl. popoverPresentationController . delegate = self ;
navCtrl. popoverPresentationController . sourceView = button;
navCtrl. popoverPresentationController . sourceRect = button. bounds ;
navCtrl. preferredContentSize = [colorSelectionController. view systemLayoutSizeFittingSize : UILayoutFittingCompressedSize ];
colorSelectionController. delegate = self ;
colorSelectionController.color = self.view.backgroundColor;
[
self
presentViewController
:navCtrl
animated
:
YES
completion
:
nil
];
我觉得两种都可以,实现的都不错,看个人喜好和界面要求。