// // 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 - 40*2; CGFloat imHeight= kScreenSize.width - 60; self.backgroundColor=[UIColor colorWithWhite:0 alpha:0.5]; _backgroundImage=[[UIImageView alloc]initWithFrame:CGRectMake(40,kScreenSize.height/2-(kScreenSize.width -30)/2, imgWid, imHeight)]; _backgroundImage.image=[UIImage imageNamed:@"Box"]; _backgroundImage.userInteractionEnabled=YES; [self addSubview:_backgroundImage]; 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]; _picView=[[UIPickerView alloc]initWithFrame:CGRectMake(10,_startLabel.frame.origin.y + 20,imgWid - 20,imHeight -30 - 40- 20)]; _picView.dataSource=self; _picView.delegate=self; [_backgroundImage addSubview:_picView]; _cancleButton=[UIButton buttonWithType:UIButtonTypeCustom]; _cancleButton.backgroundColor=[UIColor lightGrayColor]; CGFloat wdspace=40; _cancleButton.frame=CGRectMake(40,_picView.frame.origin.y + _picView.frame.size.height - 10, (_backgroundImage.frame.size.width-3*wdspace)/2, 30); [_cancleButton setTitle:@" 取消" forState:UIControlStateNormal]; _cancleButton.layer.masksToBounds=YES; _cancleButton.layer.cornerRadius=5; _cancleButton.userInteractionEnabled=YES; //[_backgroundImage addSubview:_cancleButton]; _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]; [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)dissmissView{ [self removeFromSuperview]; } -(void)initData{ _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 2; } -(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 (component==0) { _startString=_dataArr[0][_row]; }else if(component==1) { _endString=_dataArr[1][_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/2; } @end