// // MyPickerView.m // Ifish // // Created by imac on 15/10/19. // Copyright © 2015年 imac. All rights reserved. // #import "MyPickerView.h" #import "AppDelegate.h" #import "BackmassegeModel.h" @interface MyPickerView() @end @implementation MyPickerView -(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"]; [self addSubview:_backgroundImage]; _backgroundImage.userInteractionEnabled=YES; UILabel*title=[[UILabel alloc]initWithFrame:CGRectMake(10,10, _backgroundImage.frame.size.width-10*2, 20)]; title.text=@"温度设置"; title.textAlignment=NSTextAlignmentCenter; [_backgroundImage addSubview:title]; UILabel*lowLabel=[[UILabel alloc]initWithFrame:CGRectMake(10,title.frame.size.height+20, _backgroundImage.frame.size.width/2, 20)]; lowLabel.text=@"最低温度 "; lowLabel.textAlignment=NSTextAlignmentCenter; [_backgroundImage addSubview:lowLabel]; UILabel*heightLabel=[[UILabel alloc]initWithFrame:CGRectMake(_backgroundImage.frame.size.width/2,title.frame.size.height+20, _backgroundImage.frame.size.width/2, 20)]; heightLabel.text=@"最高温度"; heightLabel.textAlignment=NSTextAlignmentCenter; [_backgroundImage addSubview:heightLabel]; _picView=[[UIPickerView alloc]initWithFrame:CGRectMake(10,lowLabel.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=4; _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=COLOR_LABEL_TITLE; [_sureButton setTitle:@"确定" forState:UIControlStateNormal]; _sureButton.layer.masksToBounds=YES; _sureButton.layer.cornerRadius=4; _sureButton.userInteractionEnabled=YES; [_backgroundImage addSubview:_sureButton]; [self dataSource]; _componentArray=[[NSMutableArray alloc] init]; 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)dataSource{ _picArr=@[@[@"5",@"6",@"7",@"8",@"9",@"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"],@[@"~"],@[@"5",@"6",@"7",@"8",@"9",@"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"]]; } //#pragma mark picView数据源 #pragma mark picView数据源 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ NSInteger component; component=_picArr.count; return component; } -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ NSArray *rowArray=[[NSArray alloc] init]; rowArray=_picArr[component]; return rowArray.count; } #pragma mark picView代理 -(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ NSString *rowTitle=nil; rowTitle=_picArr[component][row]; // NSLog(@"rowtitle%@",rowTitle); _rowTitle=rowTitle; return rowTitle; } -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ // BackmassegeModel*backmodel=[[DataCenter defaultDtacenter]valueForKey:@"DeviceBackInfo"]; _component=component; _row=row; _picViewResultString=@""; if (![self.componentArray containsObject:@(component)]) { [self.componentArray addObject:@(component)]; } for (int i=0; i<_picArr.count;i++) { if ([self.componentArray containsObject:@(i)]) { NSInteger cIndex = [pickerView selectedRowInComponent:i]; _picViewResultString=[NSString stringWithFormat:@"%@%@",_picViewResultString,_picArr[i][cIndex]]; }else{ NSLog(@"%ld",(long)_heighindex); if (i==0) { _picViewResultString=[NSString stringWithFormat:@"%@%@",_picViewResultString ,_picArr[i][_lowindex]]; }else if (i==1) { _picViewResultString=[NSString stringWithFormat:@"%@%@",_picViewResultString ,_picArr[i][0]]; }else if (i==2){ _picViewResultString=[NSString stringWithFormat:@"%@%@",_picViewResultString ,_picArr[i][_heighindex]]; } } NSLog(@"pick%@",_picViewResultString); } } -(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 -40)/3; } @end