#import <UIKit/UIKit.h> @class JZNumPickerView; @protocol JZNumPickerViewDelegate <NSObject> @optional /** * 按钮点击 */ -(void)pickerView:(JZNumPickerView *)pickerView selectBtn:(UIButton*)sender; /** * 某位数据发生变化 */ -(void)pickerViewNumChange:(NSString *)numString; @end @interface JZNumPickerView : UIView @property(nonatomic,strong)UIPickerView* myPickerView; @property(nonatomic,strong)UIButton* choseKeyboard; @property(nonatomic,strong)NSString* leftBtnStr; @property(nonatomic,strong)NSString* rightBtnStr; @property(nonatomic,assign)NSInteger dataNum; // 数据位数 @property(nonatomic,weak)id<JZNumPickerViewDelegate> delegate; -(instancetype)initWithFrame:(CGRect)frame DataNum:(NSInteger)dataNum DecimalsNum:(NSInteger)decimalsNum; /** * 改变数据显示 */ -(void)changePickerViewNum:(NSString *)numString; /** * 获取当前轮盘数据 * * @return 当前数据 */ -(NSString *)getCurrentPickerViewNum; @end
JZNumPickerView.h
#import "JZNumPickerView.h" #import <math.h> #define toolHeight 40 #define buttonWidth 60 #define cusKeyboardHeight 40 #define cusKeyboardWidth 40 #define cusPointWidth 15 #define cusBorderWidth 20 #define maxNum 16380 @interface JZNumPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource> @property(nonatomic,strong)UIButton* leftBtn; @property(nonatomic,strong)UIButton* rightBtn; //纪录停止时每一位数据,方便计算数据增减总数 @property(nonatomic,strong)NSMutableArray* lastDataArr; //当前数据 @property(nonatomic,assign)NSInteger numOfNow; //最大显示数据 @property(nonatomic,assign)NSInteger numOfMax; //是否显示小数 @property(nonatomic,assign)NSInteger decimalsNum; @end @implementation JZNumPickerView /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ } -(void)setLeftBtnStr:(NSString *)leftBtnStr{ if (self.leftBtn) { [self.leftBtn setTitle:leftBtnStr forState:UIControlStateNormal]; [self.leftBtn setTitleColor:[UIColor barItemTintColor] forState:UIControlStateNormal]; } } -(void)setRightBtnStr:(NSString *)rightBtnStr{ if (self.rightBtn) { [self.rightBtn setTitle:rightBtnStr forState:UIControlStateNormal]; [self.rightBtn setTitleColor:[UIColor barItemTintColor] forState:UIControlStateNormal]; } } -(instancetype)initWithFrame:(CGRect)frame DataNum:(NSInteger)dataNum DecimalsNum:(NSInteger)decimalsNum{ if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor whiteColor]; self.opaque = NO; self.numOfNow = 0; self.dataNum = dataNum; self.decimalsNum = decimalsNum; [self creatView]; [self loadData]; } return self; } -(void)loadData{ self.lastDataArr = [[NSMutableArray alloc] init]; NSNumber *temp = [NSNumber numberWithInteger:maxNum/2]; for (NSInteger i = self.dataNum - 1; i >= 0;i--) { if (i == self.dataNum - self.decimalsNum-1) { continue; } [self.lastDataArr addObject:temp]; } [self updatePickerData]; self.numOfMax = [self getMaxNumWithNum:self.dataNum - 1]; } //-(void)updatePickerData{ // // NSInteger tempNum = self.numOfNow; // for (NSInteger i = self.dataNum - 1; i >= 0;i--) { // if (i == self.dataNum - 3) { // continue; // } // [self.myPickerView selectRow:maxNum/2 + (tempNum % 10) inComponent:i animated:NO]; // [self.lastDataArr replaceObjectAtIndex:i > self.dataNum - 3 ? i - 1 : i withObject:[NSNumber numberWithInteger:maxNum/2 + (tempNum % 10)]]; // tempNum /= 10; // } //} #warning -(void)updatePickerData{ NSInteger tempNum = self.numOfNow; for (NSInteger i = self.dataNum - 1; i >= 0;i--) { if (i == self.dataNum - self.decimalsNum-1) { continue; } [self.myPickerView selectRow:maxNum/2 + (tempNum % 10) inComponent:i animated:NO]; [self.lastDataArr replaceObjectAtIndex: i > (self.dataNum - self.decimalsNum-1) ? i - 1 : i withObject:[NSNumber numberWithInteger:maxNum/2 + (tempNum % 10)]]; tempNum /= 10; } } //#warning test //-(void)updatePickerData{ // // NSInteger tempNum = self.numOfNow; // for (NSInteger i = self.dataNum - 1; i >= 0;i--) { // if (i == self.dataNum - 3) { // continue; // } // [self.myPickerView selectRow:(tempNum % 10) inComponent:i animated:NO]; // [self.lastDataArr replaceObjectAtIndex:i > self.dataNum - 3 ? i - 1 : i withObject:[NSNumber numberWithInteger:(tempNum % 10)]]; // tempNum /= 10; // } //} -(NSInteger)getMULWithNum:(NSInteger)num{ NSInteger tempNum = 1; for (; num > 0; num--) { tempNum *= 10; } return tempNum; } -(NSInteger)getMaxNumWithNum:(NSInteger)num{ NSInteger tempNum = 0; for (; num > 0; num--) { tempNum *= 10; tempNum += 9; } return tempNum; } -(void)creatView{ self.myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, toolHeight, self.frame.size.width, self.frame.size.height - toolHeight - 35)]; [self addSubview:self.myPickerView]; self.myPickerView.delegate = self; self.myPickerView.dataSource = self; UIView *btnBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, toolHeight)]; btnBackView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0]; [self addSubview:btnBackView]; self.leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 2, buttonWidth, toolHeight - 2*2)]; [self.leftBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:self.leftBtn]; self.rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width - buttonWidth, 2, buttonWidth, toolHeight - 2*2)]; [self.rightBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:self.rightBtn]; self.choseKeyboard = [UIButton buttonWithType:UIButtonTypeCustom]; self.choseKeyboard.frame = CGRectMake(self.frame.size.width - cusKeyboardWidth, self.frame.size.height- cusKeyboardHeight, cusKeyboardWidth, cusKeyboardHeight); [self.choseKeyboard setImage:[UIImage imageNamed:@"ic_keyboard"] forState:UIControlStateNormal]; [self.choseKeyboard setImage:[UIImage imageNamed:@"ic_keyboard_press"] forState:UIControlStateHighlighted]; [self.choseKeyboard addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:self.choseKeyboard]; } -(void)changePickerViewNum:(NSString *)numString{ if (self.decimalsNum != 0) { self.numOfNow = [[NSString stringWithFormat:@"%f",[numString floatValue]*pow(10, self.decimalsNum)] floatValue]; }else { self.numOfNow = [[NSString stringWithFormat:@"%.f",[numString floatValue]] floatValue]; } // NSInteger numOfNowtest = self.numOfNow; [self updatePickerData]; } -(NSString *)getCurrentPickerViewNum{ if (self.decimalsNum != 0) { // CGFloat h = pow(10, -self.decimalsNum); return [NSString stringWithFormat:@"%f",(int)self.numOfNow * pow(10, -self.decimalsNum)]; }else { return [NSString stringWithFormat:@"%.f",(self.numOfNow*1.0)]; } //return [NSString stringWithFormat:@"%.2f",((float)self.numOfNow)*0.01]; } #pragma mark- 事件响应 -(void)btnAction:(UIButton *)sender{ if ([sender.titleLabel.text isEqualToString:@"取消"]) { [self dismissPopover]; [[NSNotificationCenter defaultCenter] postNotificationName:@"JZYBCBScrollviewfamechange" object:nil]; } if ([self.delegate respondsToSelector:@selector(pickerView:selectBtn:)]) { [self.delegate pickerView:self selectBtn:sender]; }; } #pragma mark- UIPickerViewDataSource -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return self.dataNum; } -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ if (component == self.dataNum - self.decimalsNum-1) { return 1; } return maxNum; } #pragma mark- UIPickerViewDelegate - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { return 28; } -(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ if (self.decimalsNum != 0) { if (component == (self.dataNum - (self.decimalsNum+1))) { return cusPointWidth; }; return (self.frame.size.width-self.decimalsNum*cusPointWidth-cusBorderWidth)/(self.dataNum-1); }else { return (self.frame.size.width-cusBorderWidth)/(self.dataNum+1); } } -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ if (component == (self.dataNum - self.decimalsNum-1)) { return @"."; }; return [NSString stringWithFormat:@"%ld",row % 10]; } //-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ // // //差值 // NSInteger num = row - [[self.lastDataArr objectAtIndex:component > self.dataNum - 3 ? component - 1 : component] integerValue]; // if (num <= 0) { // NSMutableString *mstr = [[NSMutableString alloc]init]; // for (int i = 0; i < self.dataNum ; i++) { // if (i != self.dataNum - 3) { // [mstr appendFormat:@"%ld",[pickerView selectedRowInComponent:i] % 10]; // } // } // self.numOfNow = [mstr integerValue]; // } // else // { // self.numOfNow += num * [self getMULWithNum:component > self.dataNum - 3 ? self.dataNum - component - 1: self.dataNum - component - 2]; // if (self.numOfNow > self.numOfMax) { // self.numOfNow -= self.numOfMax + 1; // self.numOfNow = pow(10, self.dataNum) -1; // }else if (self.numOfNow < 0){ // // self.numOfNow += self.numOfMax + 1; // } // } // [self updatePickerData]; // // if (self.delegate && [self.delegate respondsToSelector:@selector(pickerViewNumChange:)]) { // // [self.delegate pickerViewNumChange:[self getCurrentPickerViewNum]]; // } //} // #warning -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ //差值 NSInteger num = row - [[self.lastDataArr objectAtIndex:component > (self.dataNum - self.decimalsNum-1) ? component - 1 : component] integerValue]; if (num <= 0) { NSMutableString *mstr = [[NSMutableString alloc]init]; for (int i = 0; i < self.dataNum ; i++) { if (i != self.dataNum - self.decimalsNum-1) { [mstr appendFormat:@"%ld",[pickerView selectedRowInComponent:i] % 10]; } } self.numOfNow = [mstr integerValue]; } else { self.numOfNow += num * [self getMULWithNum:component > (self.dataNum - self.decimalsNum-1)? self.dataNum - component - 1: self.dataNum - component - 2]; if (self.numOfNow > self.numOfMax) { self.numOfNow -= self.numOfMax + 1; self.numOfNow = pow(10, self.dataNum) -1; // NSLog(@"现在的数%ld",self.numOfNow); }else if (self.numOfNow < 0){ self.numOfNow += self.numOfMax + 1; } } [self updatePickerData]; if (self.delegate && [self.delegate respondsToSelector:@selector(pickerViewNumChange:)]) { [self.delegate pickerViewNumChange:[self getCurrentPickerViewNum]]; } } ////更改字体大小 //- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view //{ // UILabel* pickerLabel = (UILabel*)view; // pickerLabel = [[UILabel alloc]initWithFrame:view.bounds]; // [pickerLabel setFont:[UIFont boldSystemFontOfSize:20]]; // if (self.decimalsNum != 0) { // if (component > self.dataNum -self.decimalsNum-1) // { // [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]]; // } // } // pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component]; // pickerLabel.textAlignment = NSTextAlignmentCenter; // return pickerLabel; // //} @end
JZNumPickerView.m
时间: 2024-11-06 16:45:19