ifish/Ifish/views/mypicView/MydatePickerView.m

292 lines
14 KiB
Objective-C

//
// MydatePickerView.m
// mypick
//
// Created by imac on 15/11/4.
// Copyright © 2015年 imac. All rights reserved.
//
#import "MydatePickerView.h"
#define kScreenSize [UIScreen mainScreen].bounds.size
#include "AppDelegate.h"
@implementation MydatePickerView
-(instancetype)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if (self) {
CGFloat imgWid= kScreenSize.width - 10*2;
CGFloat imHeight= kScreenSize.width - 60;
self.backgroundColor=[UIColor colorWithWhite:0 alpha:0.5];
_backgroundImage=[[UIImageView alloc]initWithFrame:CGRectMake(10,kScreenSize.height/2-(kScreenSize.width -30)/2, imgWid, imHeight)];
_backgroundImage.image=[UIImage imageNamed:@"Box"];
_backgroundImage.userInteractionEnabled=YES;
[self addSubview:_backgroundImage];
_backgroundImage.layer.masksToBounds=YES;
_backgroundImage.layer.cornerRadius=10;
UILabel*title=[[UILabel alloc]initWithFrame:CGRectMake(10,10, _backgroundImage.frame.size.width-10*2, 20)];
title.text=@"时间设置";
title.textAlignment=NSTextAlignmentCenter;
[_backgroundImage addSubview:title];
_startLabel=[[UILabel alloc] initWithFrame:CGRectMake(10,title.frame.size.height+20, _backgroundImage.frame.size.width/2, 20)];
_startLabel.text=@"开始时间";
_startLabel.font=[UIFont systemFontOfSize:15];
_startLabel.textAlignment=NSTextAlignmentCenter;
[_backgroundImage addSubview:_startLabel];
_endLabel=[[UILabel alloc] initWithFrame:CGRectMake(_backgroundImage.frame.size.width/2,title.frame.size.height+20, _backgroundImage.frame.size.width/2, 20)];
_endLabel.text=@"结束时间";
_endLabel.font=[UIFont systemFontOfSize:15];
_endLabel.textAlignment=NSTextAlignmentCenter;
[_backgroundImage addSubview:_endLabel];
_startString = _startString1 =_startSecondString= _endString = _endString1 = _endSecondString= @"00";
_picView=[[UIPickerView alloc]initWithFrame:CGRectMake(0,_startLabel.frame.origin.y + 20,imgWid,imHeight -30 - 40- 20)];
_picView.dataSource=self;
_picView.delegate=self;
[_backgroundImage addSubview:_picView];
_cancleButton=[UIButton buttonWithType:UIButtonTypeCustom];
_cancleButton.backgroundColor=[UIColor lightGrayColor];
CGFloat wdspace=10;
_sureButton
=[UIButton buttonWithType:UIButtonTypeCustom];
_sureButton.frame=CGRectMake(0, _backgroundImage.frame.size.height - 44 + 3, _backgroundImage.frame.size.width, 44);
_sureButton.backgroundColor=[UIColor blueColor];
[_sureButton setTitle:@"确定" forState:UIControlStateNormal];
// [_sureButton addTarget:self action:@selector(sureButtonClick) forControlEvents:UIControlEventTouchUpInside];
_sureButton.backgroundColor=COLOR_LABEL_TITLE;
_sureButton.layer.masksToBounds=YES;
_sureButton.layer.cornerRadius=5;
_sureButton.userInteractionEnabled=YES;
[_backgroundImage addSubview:_sureButton];
_cancleButton.frame=CGRectMake(0,0,0,0);
[_cancleButton setTitle:@"删除" forState:UIControlStateNormal];
// _cancleButton.layer.masksToBounds=YES;
// _cancleButton.layer.cornerRadius=5;
// _cancleButton.userInteractionEnabled=YES;
// _cancleButton.backgroundColor = [UIColor purpleColor];
_cancleButton.hidden = YES;
[_backgroundImage addSubview:_cancleButton];
[self initData];
UITapGestureRecognizer *sigleTap=[[UITapGestureRecognizer alloc] init ];
[self addGestureRecognizer:sigleTap];
CGPoint locationP= [sigleTap locationInView:self];
BOOL isIn= CGRectContainsPoint( _backgroundImage.frame ,locationP);
if (!isIn) {
[sigleTap addTarget:self action:@selector(dissmissView)];
}
}
return self;
}
- (void)setPickerType:(NSString *)pickerType {
_pickerType = pickerType;
[self initData];
if ([self.pickerType isEqualToString:@"xuanduo"])
{
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(12,_startLabel.frame.origin.y+30, _picView.frame.size.width/4, 20)];
label1.text=@"小时";
label1.font=[UIFont systemFontOfSize:15];
label1.textAlignment=NSTextAlignmentCenter;
[_backgroundImage addSubview:label1];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(12+label1.frame.size.width+2,_startLabel.frame.origin.y+30, _picView.frame.size.width/4, 20)];
label2.text=@"分钟";
label2.font=[UIFont systemFontOfSize:15];
label2.textAlignment=NSTextAlignmentCenter;
[_backgroundImage addSubview:label2];
UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(label2.frame.origin.x+label2.frame.size.width+ 4,_startLabel.frame.origin.y+30, _picView.frame.size.width/4, 20)];
label3.text=@"小时";
label3.font=[UIFont systemFontOfSize:15];
label3.textAlignment=NSTextAlignmentCenter;
[_backgroundImage addSubview:label3];
UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(label3.frame.origin.x+label3.frame.size.width+2,_startLabel.frame.origin.y+30, _picView.frame.size.width/4, 20)];
label4.text=@"分钟";
label4.font=[UIFont systemFontOfSize:15];
label4.textAlignment=NSTextAlignmentCenter;
[_backgroundImage addSubview:label4];
}
else
{
_startLabel.text=@"开启时间";
_endLabel.text=@"关闭时间";
[_cancleButton setTitle:@"取消" forState:UIControlStateNormal];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(12,_startLabel.frame.origin.y+30, _picView.frame.size.width/6, 20)];
label1.text=@"";
label1.font=[UIFont systemFontOfSize:15];
label1.textAlignment=NSTextAlignmentCenter;
[_backgroundImage addSubview:label1];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label1.frame),_startLabel.frame.origin.y+30, _picView.frame.size.width/6, 20)];
label2.text=@"";
label2.font=[UIFont systemFontOfSize:15];
label2.textAlignment=NSTextAlignmentCenter;
[_backgroundImage addSubview:label2];
UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label2.frame),_startLabel.frame.origin.y+30, _picView.frame.size.width/6, 20)];
label3.text=@"";
label3.font=[UIFont systemFontOfSize:15];
label3.textAlignment=NSTextAlignmentCenter;
[_backgroundImage addSubview:label3];
UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label3.frame),_startLabel.frame.origin.y+30, _picView.frame.size.width/6, 20)];
label4.text=@"";
label4.font=[UIFont systemFontOfSize:15];
label4.textAlignment=NSTextAlignmentCenter;
[_backgroundImage addSubview:label4];
UILabel *label5 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label4.frame),_startLabel.frame.origin.y+30, _picView.frame.size.width/6, 20)];
label5.text=@"";
label5.font=[UIFont systemFontOfSize:15];
label5.textAlignment=NSTextAlignmentCenter;
[_backgroundImage addSubview:label5];
UILabel *label6 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label5.frame),_startLabel.frame.origin.y+30, _picView.frame.size.width/6, 20)];
label6.text=@"";
label6.font=[UIFont systemFontOfSize:15];
label6.textAlignment=NSTextAlignmentCenter;
[_backgroundImage addSubview:label6];
}
[_picView reloadAllComponents];
}
- (void)setHaveDelected:(BOOL)haveDelected {
_haveDelected = haveDelected;
_cancleButton.hidden = NO;
[_sureButton setBackgroundImage:[UIImage imageNamed:@"surebtn_bg"] forState:UIControlStateNormal];
[_cancleButton setBackgroundImage:[UIImage imageNamed:@"deletebtn_bg"] forState:UIControlStateNormal];
CGRect pickerframe = _picView.frame;
pickerframe.origin.y = pickerframe.origin.y + 8;
_picView.frame = pickerframe;
CGRect frame = _sureButton.frame;
frame.size.width = frame.size.width/2.0;
_sureButton.frame = frame;
_sureButton.layer.cornerRadius=0;
_cancleButton.frame = CGRectMake(CGRectGetMaxX(frame), frame.origin.y, frame.size.width, frame.size.height);
}
-(void)dissmissView{
[self removeFromSuperview];
}
-(void)initData{
if ([self.pickerType isEqualToString:@"xuanduo"]) {
_startString = _startString1 = _endString = _endString1 = @"00";
_dataArr = @[@[@"00",@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14"
,@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23"],@[@"00",@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31",@"32",@"33",@"34",@"35",@"36",@"37",@"38",@"39",@"40",@"41",@"42",@"43",@"44",@"45",@"46",@"47",@"48",@"49",@"50",@"51",@"52",@"53",@"54",@"55",@"56",@"57",@"58",@"59"],@[@"00",@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23"],@[@"00",@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31",@"32",@"33",@"34",@"35",@"36",@"37",@"38",@"39",@"40",@"41",@"42",@"43",@"44",@"45",@"46",@"47",@"48",@"49",@"50",@"51",@"52",@"53",@"54",@"55",@"56",@"57",@"58",@"59"]];
}else if ([self.pickerType isEqualToString:@"cycleType"])
{
_startString = _startString1 = _endString = _endString1 = @"00";
_dataArr = @[@[@"00",@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14"
,@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23"],@[@"00",@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31",@"32",@"33",@"34",@"35",@"36",@"37",@"38",@"39",@"40",@"41",@"42",@"43",@"44",@"45",@"46",@"47",@"48",@"49",@"50",@"51",@"52",@"53",@"54",@"55",@"56",@"57",@"58",@"59"],@[@"00",@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31",@"32",@"33",@"34",@"35",@"36",@"37",@"38",@"39",@"40",@"41",@"42",@"43",@"44",@"45",@"46",@"47",@"48",@"49",@"50",@"51",@"52",@"53",@"54",@"55",@"56",@"57",@"58",@"59"],@[@"00",@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23"],@[@"00",@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31",@"32",@"33",@"34",@"35",@"36",@"37",@"38",@"39",@"40",@"41",@"42",@"43",@"44",@"45",@"46",@"47",@"48",@"49",@"50",@"51",@"52",@"53",@"54",@"55",@"56",@"57",@"58",@"59"],@[@"00",@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31",@"32",@"33",@"34",@"35",@"36",@"37",@"38",@"39",@"40",@"41",@"42",@"43",@"44",@"45",@"46",@"47",@"48",@"49",@"50",@"51",@"52",@"53",@"54",@"55",@"56",@"57",@"58",@"59"]];
}
else {
_dataArr=@[@[@"0:00",@"0:30",@"1:00",@"1:30",@"2:00",@"2:30",@"3:00",@"3:30",@"4:00",@"4:30",@"5:00",@"5:30",@"6:00",@"6:30",@"7:00",@"7:30",@"8:00",@"8:30",@"9:00",@"9:30",@"10:00",@"10:30",@"11:00",@"11:30",@"12:00",@"12:30",@"13:00",@"13:30",@"14:00",@"14:30",@"15:00",@"15:30",@"16:00",@"16:30",@"17:00",@"17:30",@"18:00",@"18:30",@"19:00",@"19:30",@"20:00",@"20:30",@"21:00",@"21:30",@"22:00",@"22:30",@"23:00",@"23:30"],@[@"0:29",@"0:59",@"1:29",@"1:59",@"2:29",@"2:59",@"3:29",@"3:59",@"4:29",@"4:59",@"5:29",@"5:59",@"6:29",@"6:59",@"7:29",@"7:59",@"8:29",@"8:59",@"9:29",@"9:59",@"10:29",@"10:59",@"11:29",@"11:59",@"12:29",@"12:59",@"13:29",@"13:59",@"14:29",@"14:59",@"15:29",@"15:59",@"16:29",@"16:59",@"17:29",@"17:59",@"18:29",@"18:59",@"19:29",@"19:59",@"20:29",@"20:59",@"21:29",@"21:59",@"22:29",@"22:59",@"23:29",@"23:59",]];
}
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
NSArray *rowArray=[[NSArray alloc] init];
rowArray=_dataArr[component];
return rowArray.count;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return _dataArr.count;
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSString *rowTitle=nil;
rowTitle=_dataArr[component][row];
return rowTitle;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
_row=row;
_compennent=component;
if ([self.pickerType isEqualToString:@"cycleType"])
{
if (component==0) {
_startString=_dataArr[0][_row];
}else if(component==1) {
_endString=_dataArr[1][_row];
}
else if (component==2){
_startSecondString=_dataArr[2][_row];
}else if (component==3){
_startString1=_dataArr[3][_row];
}
else if (component==4){
_endString1=_dataArr[4][_row];
}
else if (component==5){
_endSecondString=_dataArr[5][_row];
}
}
else
{
if (component==0) {
_startString=_dataArr[0][_row];
}else if(component==1) {
_endString=_dataArr[1][_row];
}else if (component==2){
_startString1=_dataArr[2][_row];
}else if (component==3){
_endString1=_dataArr[3][_row];
}
}
// [_picView selectRow:row inComponent:component animated:YES];
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return pickerView.frame.size.height/5;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
return pickerView.frame.size.width/_dataArr.count;
}
@end