UIPickerView的简单使用
2017-03-21 本文已影响38人
郑州程序员王一
从写了营业时间的界面,用到了小时、分钟
Simulator Screen Shot 2017年3月21日 下午9.59.50.png//
// RBSetWorkTimeVc.m
// HuiHomeRestaurant
//
// Created by 王一 on 2017/3/21.
// Copyright © 2017年 wangyi. All rights reserved.
//
#import "RBSetWorkTimeVc.h"
#import "NSString+RBExtension.h"
@interface RBSetWorkTimeVc ()<UIPickerViewDataSource,UIPickerViewDelegate>
//遵循协议
@property (nonatomic,strong)UIPickerView * pickerView;//自定义pickerview
@property (nonatomic,strong)NSArray *hourArray;//保存的小时
@property (nonatomic,strong)NSArray *minuteArray;//保存要展示的数字
//上班
@property(nonatomic,strong)UILabel *openLabel;
//下班
@property(nonatomic,strong)UILabel *closeLabel;
//阴影模板
@property (nonatomic,strong)UIView *shadowView;
//内容模板
@property (nonatomic,strong)UIView *contentView;
//分享的View高度
@property(nonatomic,assign)CGFloat ShareViewHeight;
//上班时间View
@property(nonatomic,strong) UIView *openView;
//下班时间View
@property(nonatomic,strong) UIView *closeView;
//UIPickerView 小时字符串
@property(nonatomic,copy)NSString *openStr;
//UIPickerView 分钟字符串
@property(nonatomic,copy)NSString *closeStr;
//区分上下班显示
@property(nonatomic,assign)NSInteger buttonTag;
@end
@implementation RBSetWorkTimeVc
- (void)viewDidLoad {
[super viewDidLoad];
//设置导航栏
[self initNavigation];
//初始化视图
[self initView];
}
//设置导航栏
-(void)initNavigation{
self.navigationItem.title = @"营业时间";
self.view.backgroundColor = WYGlobalBg;
}
//初始化视图
-(void)initView{
//上班时间View
self.openView = [[UIView alloc]initWithFrame:CGRectMake(0, 0 + 64, WYScreenW, 50)];
self.openView.backgroundColor = [UIColor whiteColor];
self.openView.userInteractionEnabled=YES;
UITapGestureRecognizer *openViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(openViewClick)];
[self.openView addGestureRecognizer:openViewTap];
[self.view addSubview:self.openView];
//文字
self.openLabel = [[UILabel alloc]init];
[self setLabel:self.openLabel WithFont:15];
self.openLabel.text = [NSString stringWithFormat:@"上班时间: %@",self.openTime];
[self.openView addSubview:self.openLabel];
[self.openLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.openView.mas_left).offset(10);
make.centerY.equalTo(self.openView.mas_centerY);
}];
//下班时间View
self.closeView = [[UIView alloc]initWithFrame:CGRectMake(0, 50 + 64 + 1, WYScreenW, 50)];
self.closeView.backgroundColor = [UIColor whiteColor];
self.closeView.userInteractionEnabled=YES;
UITapGestureRecognizer *closeViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeViewClick)];
[self.closeView addGestureRecognizer:closeViewTap];
[self.view addSubview:self.closeView];
//文字
self.closeLabel = [[UILabel alloc]init];
[self setLabel:self.closeLabel WithFont:15];
self.closeLabel.text = [NSString stringWithFormat:@"下班时间: %@",self.closeTime];
[self.closeView addSubview:self.closeLabel];
[self.closeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.closeView.mas_left).offset(10);
make.centerY.equalTo(self.closeView.mas_centerY);
}];
//提交按钮
BButton *submitBtn = [[BButton alloc]initWithFrame:CGRectMake(10, WYScreenH - 120, WYScreenW - 20, 40) color:WYRGBColor(255, 45, 80) style:BButtonStyleBootstrapV3];
[submitBtn setTitle:@"提 交" forState:UIControlStateNormal];
[submitBtn addTarget:self action:@selector(dismissShareViewFinish) forControlEvents:UIControlEventTouchUpInside];
[submitBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
[self.view addSubview:submitBtn];
}
-(void)setLabel:(UILabel *)label WithFont:(NSInteger)font{
label.textColor = WYRGBColor(51, 51, 51);
label.font = [UIFont systemFontOfSize:font];
}
//上班点击
-(void)openViewClick{
self.buttonTag = 20000;
//创建 pickerView
[self initPickerView];
//上下班时间默认值方法
[self setCutWithString:self.openTime];
}
//下班点击
-(void)closeViewClick{
self.buttonTag = 20001;
//创建 pickerView
[self initPickerView];
//上下班时间默认值方法
[self setCutWithString:self.closeTime];
}
//上下班时间默认值方法
-(void)setCutWithString:(NSString *)String{
NSString *frontStr = @"0";
NSString *backStr = @"0";
if (![NSString isBlankString:String]) {
for (int i = 0; i < String.length; i++) {
NSString *str = [String substringWithRange:NSMakeRange(i, 1)];
if ([str isEqualToString:@":"]) {
frontStr = [String substringWithRange:NSMakeRange(0,i)];
NSInteger j = i + 1;
backStr = [String substringWithRange:NSMakeRange(j,String.length - j)];
}
}
}
[self.pickerView selectRow:[frontStr intValue] inComponent:0 animated:NO];
[self.pickerView selectRow:[backStr intValue] inComponent:1 animated:NO];
self.openStr = frontStr;
self.closeStr = backStr;
}
//关闭阴影
-(void)dismissShareView{
[UIView animateWithDuration:0.3 animations:^{
self.shadowView.alpha = 0.0;
self.contentView.frame = CGRectMake(0, WYScreenH, WYScreenW, self.ShareViewHeight);
} completion:^(BOOL finished) {
//移除
UIView *shadowView = [self.shadowView viewWithTag:10000];
[shadowView removeFromSuperview];
UIView *contentView = [self.contentView viewWithTag:10001];
[contentView removeFromSuperview];
}];
}
//创建 pickerView
-(void)initPickerView{
//阴影
self.ShareViewHeight = 250;
self.shadowView = [[UIView alloc] initWithFrame:self.view.bounds];
self.shadowView.backgroundColor = [UIColor blackColor];
self.shadowView.tag = 10000;
self.shadowView.alpha = 0.0;
[self.shadowView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissShareView)]];
[self.view addSubview:self.shadowView];
//容器View
self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, WYScreenH, WYScreenW, self.ShareViewHeight)];
self.contentView.tag = 10001;
self.contentView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.contentView];
//需要展示的数据以数组的形式保存
self.hourArray = @[@"00",@"01", @"02", @"03", @"04", @"05", @"06", @"07",
@"08", @"09", @"10", @"11", @"12", @"13", @"14",
@"15", @"16", @"17", @"18", @"19", @"20", @"21",
@"22", @"23"];
self.minuteArray = @[@"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"];
// 初始化pickerView
self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 50, WYScreenW, 200)];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
[self.contentView addSubview:self.pickerView];
//取消按钮
BButton *cancelBtn = [[BButton alloc]initWithFrame:CGRectMake(20, 10, 60, 30) type:BButtonTypeDefault style:BButtonStyleBootstrapV3];
[cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
[cancelBtn addTarget:self action:@selector(dismissShareView) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:cancelBtn];
//确定按钮
BButton *finishBtn = [[BButton alloc]initWithFrame:CGRectMake(WYScreenW - 80, 10, 60, 30) type:BButtonTypeDefault style:BButtonStyleBootstrapV3];
[finishBtn setTitle:@"确定" forState:UIControlStateNormal];
[finishBtn addTarget:self action:@selector(dismissShareView) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:finishBtn];
[UIView animateWithDuration:0.3 animations:^{
self.shadowView.alpha = 0.3;
self.contentView.frame = CGRectMake(0, WYScreenH - self.ShareViewHeight, WYScreenW, self.ShareViewHeight);
}];
}
//提交按钮
-(void)dismissShareViewFinish{
//参数
NSString *store_id = [NSString stringWithFormat:@"%@",[UserDefaults getUserInfo][@"store_id"]];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"app"] = @"store";
params[@"func"] = @"updateOpeationTime";
params[@"store_id"] = store_id;
params[@"order_time_start"] = self.openTime;
params[@"order_time_end"] = self.closeTime;
//请求
[RB_NetAPIManager RB_PostRequestWithparams:params success:^(id json) {
if ([json[@"status"] intValue] == 1) {
[SVProgressHUD setMinimumDismissTimeInterval:1.0];
[SVProgressHUD showSuccessWithStatus:json[@"msg"]];
}
} failure:^(NSError *error) {
}];
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - UIPickerView代理方法
//指定pickerview有几个表盘
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
//指定每个表盘上有几行数据
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSInteger result = 0;
switch (component) {
case 0:
result = self.hourArray.count;//根据数组的元素个数返回几行数据
break;
case 1:
result = self.minuteArray.count;
break;
default:
break;
}
return result;
}
//指定每行如何展示数据(此处和tableview类似)
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString * title = nil;
switch (component) {
case 0:{
title = self.hourArray[row];
}break;
case 1:{
title = self.minuteArray[row];
}break;
default:
break;
}
return title;
}
//选择了那一行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (component == 0) {
self.openStr = self.hourArray[row];
}else{
self.closeStr = self.minuteArray[row];
}
if (self.buttonTag == 20000) {
self.openTime = [NSString stringWithFormat:@"%@:%@",self.openStr,self.closeStr];
self.openLabel.text = [NSString stringWithFormat:@"上班时间: %@",self.openTime];
}
if (self.buttonTag == 20001) {
self.closeTime = [NSString stringWithFormat:@"%@:%@",self.openStr,self.closeStr];
self.closeLabel.text = [NSString stringWithFormat:@"下班时间: %@",self.closeTime];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end