#define RGB(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define RGBA(r, g, b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
#import "STPickerView.h"
@implementation STPickerView
#pragma mark - --- init 视图初始化 ---
- (instancetype)initWithRow:(NSInteger )rows
{
self = [super init];
if (self) {
_rows = rows;
[self setupDefault];
[self setupUI];
}
return self;
}
- (void)setupDefault
{
// 1.设置数据的默认值
_title = nil;
_font = [UIFont systemFontOfSize:15];
_titleColor = [UIColor blackColor];
_borderButtonColor = RGB(205, 205, 205);
_heightPicker = 240;
_contentMode = STPickerContentModeBottom;
// 2.设置自身的属性
self.bounds = [UIScreen mainScreen].bounds;
self.backgroundColor = RGBA(0, 0, 0, 102.0/255);
self.layer.opacity = 0.0;
[self addTarget:self action:@selector(remove) forControlEvents:UIControlEventTouchUpInside];
// 3.添加子视图
[self addSubview:self.contentView];
[self.contentView addSubview:self.lineView];
[self.contentView addSubview:self.pickerView];
[self.contentView addSubview:self.buttonLeft];
[self.contentView addSubview:self.buttonRight];
[self.contentView addSubview:self.labelTitle];
[self.contentView addSubview:self.lineViewDown];
[self addSubview:self.btnView];
}
- (void)setupUI
{
}
- (void)layoutSubviews
{
[super layoutSubviews];
if (self.contentMode == STPickerContentModeBottom) {
}else {
self.buttonLeft.y = self.lineViewDown.bottom + 5;
self.buttonRight.y = self.lineViewDown.bottom + 5;
}
}
#pragma mark - --- delegate 视图委托 ---
#pragma mark - --- event response 事件相应 ---
- (void)selectedOk
{
[self remove];
}
- (void)selectedCancel
{
[self remove];
}
#pragma mark - --- private methods 私有方法 ---
- (void)show
{
[[UIApplication sharedApplication].keyWindow addSubview:self];
[self setCenter:[UIApplication sharedApplication].keyWindow.center];
[[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
if (self.contentMode == STPickerContentModeBottom) {
CGRect frameContent = self.contentView.frame;
frameContent.origin.y -= self.contentView.height;
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.layer setOpacity:1.0];
self.contentView.frame = frameContent;
} completion:^(BOOL finished) {
}];
}else {
CGRect frameContent = self.contentView.frame;
frameContent.origin.y -= (kScreenHeight+self.contentView.height)/2;
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.layer setOpacity:1.0];
self.contentView.frame = frameContent;
} completion:^(BOOL finished) {
}];
}
}
- (void)showWithBtnArray:(NSArray *)btnArr
{
[[UIApplication sharedApplication].keyWindow addSubview:self];
[self setCenter:[UIApplication sharedApplication].keyWindow.center];
[[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
_btnView.hidden = NO;
CGFloat btnW = (kScreenWidth - btnArr.count - 1)/btnArr.count;
for (int i = 0; i < btnArr.count; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(btnW * i + 1, 0, btnW, 34);
[btn setTitle:btnArr[i] forState:UIControlStateNormal];
[btn setTitleColor:rgb(33, 33, 33) forState:UIControlStateNormal];
btn.tag = i;
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.btnView addSubview:btn];
}
if (self.contentMode == STPickerContentModeBottom) {
CGRect frameContent = self.contentView.frame;
frameContent.origin.y -= self.contentView.height;
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.layer setOpacity:1.0];
self.contentView.frame = frameContent;
} completion:^(BOOL finished) {
}];
}else {
CGRect frameContent = self.contentView.frame;
frameContent.origin.y -= (kScreenHeight+self.contentView.height)/2;
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.layer setOpacity:1.0];
self.contentView.frame = frameContent;
} completion:^(BOOL finished) {
}];
}
}
- (void)btnAction:(UIButton *)btn{
_rows = btn.tag + 1;
[self setupUI];
}
- (void)remove
{
if (self.contentMode == STPickerContentModeBottom) {
CGRect frameContent = self.contentView.frame;
frameContent.origin.y += self.contentView.height;
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.layer setOpacity:0.0];
self.contentView.frame = frameContent;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}else {
CGRect frameContent = self.contentView.frame;
frameContent.origin.y += (kScreenHeight+self.contentView.height)/2;
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.layer setOpacity:0.0];
self.contentView.frame = frameContent;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
}
#pragma mark - --- setters 属性 ---
- (void)setTitle:(NSString *)title
{
_title = title;
[self.labelTitle setText:title];
}
- (void)setFont:(UIFont *)font
{
_font = font;
[self.buttonLeft.titleLabel setFont:font];
[self.buttonRight.titleLabel setFont:font];
[self.labelTitle setFont:font];
}
- (void)setTitleColor:(UIColor *)titleColor
{
_titleColor = titleColor;
[self.labelTitle setTextColor:titleColor];
[self.buttonLeft setTitleColor:titleColor forState:UIControlStateNormal];
[self.buttonRight setTitleColor:titleColor forState:UIControlStateNormal];
}
- (void)setBorderButtonColor:(UIColor *)borderButtonColor
{
_borderButtonColor = borderButtonColor;
// [self.buttonLeft addBorderColor:borderButtonColor];
// [self.buttonRight addBorderColor:borderButtonColor];
}
- (void)setHeightPicker:(CGFloat)heightPicker
{
_heightPicker = heightPicker;
self.contentView.height = heightPicker;
}
- (void)setContentMode:(STPickerContentMode)contentMode
{
_contentMode = contentMode;
if (contentMode == STPickerContentModeCenter) {
self.contentView.height += 44;
}
}
#pragma mark - --- getters 属性 ---
- (UIView *)contentView
{
if (!_contentView) {
CGFloat contentX = 0;
CGFloat contentY = kScreenHeight;
CGFloat contentW = kScreenWidth;
CGFloat contentH = self.heightPicker;
_contentView = [[UIView alloc]initWithFrame:CGRectMake(contentX, contentY, contentW, contentH)];
[_contentView setBackgroundColor:[UIColor whiteColor]];
}
return _contentView;
}
- (UIView *)btnView{
if (!_btnView){
_btnView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - self.heightPicker - 35, self.contentView.width, 35)];
_btnView.backgroundColor = [UIColor whiteColor];
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 34, kScreenWidth, 1)];
line.backgroundColor = rgb(240, 240, 240);
[_btnView addSubview:line];
_btnView.hidden = YES;
}
return _btnView;
}
- (UIView *)lineView
{
if (!_lineView) {
CGFloat lineX = 0;
CGFloat lineY = 44;
CGFloat lineW = self.contentView.width;
CGFloat lineH = 0.5;
_lineView = [[UIView alloc]initWithFrame:CGRectMake(lineX, lineY, lineW, lineH)];
[_lineView setBackgroundColor:self.borderButtonColor];
}
return _lineView;
}
- (UIPickerView *)pickerView
{
if (!_pickerView) {
CGFloat pickerW = self.contentView.width;
CGFloat pickerH = self.contentView.height - self.lineView.bottom;
CGFloat pickerX = 0;
CGFloat pickerY = self.lineView.bottom;
_pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(pickerX, pickerY, pickerW, pickerH)];
[_pickerView setBackgroundColor:[UIColor whiteColor]];
}
return _pickerView;
}
- (UIButton *)buttonLeft
{
if (!_buttonLeft) {
CGFloat leftW = 36;
CGFloat leftH = self.lineView.top - 10;
CGFloat leftX = 16;
CGFloat leftY = (self.lineView.top - leftH) / 2;
_buttonLeft = [[UIButton alloc]initWithFrame:CGRectMake(leftX, leftY, leftW, leftH)];
[_buttonLeft setTitle:@"取消" forState:UIControlStateNormal];
[_buttonLeft setTitleColor:self.titleColor forState:UIControlStateNormal];
// [_buttonLeft addBorderColor:self.borderButtonColor];
[_buttonLeft.titleLabel setFont:self.font];
[_buttonLeft addTarget:self action:@selector(selectedCancel) forControlEvents:UIControlEventTouchUpInside];
}
return _buttonLeft;
}
- (UIButton *)buttonRight
{
if (!_buttonRight) {
CGFloat rightW = self.buttonLeft.width;
CGFloat rightH = self.buttonLeft.height;
CGFloat rightX = self.contentView.width - rightW - self.buttonLeft.x;
CGFloat rightY = self.buttonLeft.y;
_buttonRight = [[UIButton alloc]initWithFrame:CGRectMake(rightX, rightY, rightW, rightH)];
[_buttonRight setTitle:@"确定" forState:UIControlStateNormal];
[_buttonRight setTitleColor:self.titleColor forState:UIControlStateNormal];
// [_buttonRight addBorderColor:self.borderButtonColor];
[_buttonRight.titleLabel setFont:self.font];
[_buttonRight addTarget:self action:@selector(selectedOk) forControlEvents:UIControlEventTouchUpInside];
}
return _buttonRight;
}
- (UILabel *)labelTitle
{
if (!_labelTitle) {
CGFloat titleX = self.buttonLeft.right + 5;
CGFloat titleY = 0;
CGFloat titleW = self.contentView.width - titleX * 2;
CGFloat titleH = self.lineView.top;
_labelTitle = [[UILabel alloc]initWithFrame:CGRectMake(titleX, titleY, titleW, titleH)];
[_labelTitle setTextAlignment:NSTextAlignmentCenter];
[_labelTitle setTextColor:self.titleColor];
[_labelTitle setFont:self.font];
_labelTitle.adjustsFontSizeToFitWidth = YES;
}
return _labelTitle;
}
- (UIView *)lineViewDown
{
if (!_lineViewDown) {
CGFloat lineX = 0;
CGFloat lineY = self.pickerView.bottom;
CGFloat lineW = self.contentView.width;
CGFloat lineH = 0.5;
_lineViewDown = [[UIView alloc]initWithFrame:CGRectMake(lineX, lineY, lineW, lineH)];
[_lineViewDown setBackgroundColor:self.borderButtonColor];
}
return _lineViewDown;
}
@end