iPhone开发UI组件UIDatePickerView的预习
来源:互联网 发布:js函数返回值return 编辑:程序博客网 时间:2024/06/05 07:30
新建工程如下:
编辑控制器的.h文件如下所示:
//// ViewController.h// UIDatePickerVIew//// Created by apple on 15/9/2.// Copyright (c) 2015年 LiuXun. All rights reserved.//#import <UIKit/UIKit.h>@interface ViewController : UIViewController <UIPickerViewDataSource,UIPickerViewDelegate>{ int a; int b; int t;}@property (nonatomic, strong) UIPickerView *pickView;@property (nonatomic, strong) NSMutableArray *pickerNameArray;@property (nonatomic, strong) NSMutableArray *pickerPlaceArray;@property (nonatomic, strong) NSMutableDictionary *typeDictionary;@property (nonatomic, strong) UILabel * lblSelect;@property (nonatomic, strong) NSString *selectStr;@property (nonatomic, strong) NSTimer * timer;@property (nonatomic, strong) UIButton *btn;@property (nonatomic, strong) UIImageView *zhen;@property (nonatomic, strong) UIImageView *pan;@end编辑控制器的.m文件如下所示:
//// ViewController.m// UIDatePickerVIew//// Created by apple on 15/9/2.// Copyright (c) 2015年 LiuXun. All rights reserved.//#import "ViewController.h"#define WIDTH [UIScreen mainScreen].bounds.size.width#define HEIGHT [UIScreen mainScreen].bounds.size.height@interface ViewController ()@property (nonatomic, strong) NSString *str1;@property (nonatomic, strong) NSString *str2;@property (nonatomic, strong) NSDate *start;@property (nonatomic, strong) NSDate *end;@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad];[self assignForPickView]; self.pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(10, WIDTH/2+50, WIDTH-20, WIDTH/3)]; self.pickView.backgroundColor = [UIColor yellowColor]; self.pickView.dataSource = self; self.pickView.delegate = self; self.pickView.showsSelectionIndicator = YES; [self.view addSubview:self.pickView]; // 显示数据 self.lblSelect = [[UILabel alloc ] initWithFrame:CGRectMake(0, 10, WIDTH, WIDTH/6)]; self.lblSelect .text = self.selectStr; self.lblSelect.textAlignment = NSTextAlignmentCenter; [self.view addSubview:self.lblSelect]; // button self.btn = [[UIButton alloc] initWithFrame:CGRectMake(10, WIDTH/2+WIDTH/3+100, WIDTH-20, WIDTH/5)]; [self.btn setTitle:@"点击抽奖" forState:UIControlStateNormal]; [self.btn setTitle:@"开始抽奖" forState:UIControlStateHighlighted]; [self.btn addTarget:self action:@selector(chouJiang) forControlEvents:UIControlEventTouchUpInside]; [self.btn setBackgroundColor:[UIColor redColor]]; [self.view addSubview:self.btn]; // 显示表盘 self.pan = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, (WIDTH-40)/2, (WIDTH-40)/2)]; self.pan.center = CGPointMake(WIDTH/2, 120); self.pan.image = [UIImage imageNamed:@"1.png"]; [self.view addSubview:self.pan]; // 显示表针 self.zhen = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 11, (WIDTH - 40)/4)]; self.zhen.center = CGPointMake(WIDTH/2, 120); self.zhen.image = [UIImage imageNamed:@"3.png"]; [self.view addSubview:self.zhen]; [self.view bringSubviewToFront:self.lblSelect]; } //抽奖监听方法-(void)chouJiang{ self.start = [[NSDate alloc] init]; NSLog(@"t1= %d",t); self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(jiang) userInfo:nil repeats:YES]; }-(void) jiang{ self.end = [[NSDate alloc] init]; t = self.start.timeIntervalSince1970; int t2 = self.end.timeIntervalSince1970; NSLog(@"t2= %d t1= %d",t2,t); if ((t2 - t) == 5 ) { [self.timer invalidate]; } int b1 = arc4random()%100+100000; for (int i = 0; i<b1 ; i++) { self.zhen.transform = CGAffineTransformRotate(self.zhen.transform, -M_PI_4/180); } a = arc4random() % (self.pickerNameArray.count); NSMutableArray *BB = [self.typeDictionary objectForKey:self.pickerNameArray[a]]; b = arc4random() % (BB.count); [self pickerView:self.pickView didSelectRow:a inComponent:0]; [self pickerView:self.pickView didSelectRow:b inComponent:1]; [self.pickView reloadComponent:0]; [self.pickView selectRow:a inComponent:0 animated:YES]; }-(void) assignForPickView{ self.pickerNameArray =[ [NSMutableArray alloc] initWithObjects:@"河南",@"北京",@"山东",@"河北" ,nil]; NSMutableArray *hArr = [NSMutableArray arrayWithObjects:@"周口", @"郑州",@"焦作",@"驻马店",nil]; NSMutableArray *bArr = [NSMutableArray arrayWithObjects:@"昌平",@"海淀",@"朝阳",@"丰台",@"大兴", nil]; NSMutableArray *sArr = [NSMutableArray arrayWithObjects:@"青岛",@"烟台", nil]; NSMutableArray *hBArr = [NSMutableArray arrayWithObjects:@"石家庄", nil]; NSMutableArray *typeArr = [NSMutableArray arrayWithObjects:hArr,bArr,sArr,hBArr, nil]; self.typeDictionary = [NSMutableDictionary dictionaryWithObjects:typeArr forKeys:self.pickerNameArray]; self.pickerPlaceArray = hArr; }// 分为多少列-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 2; } // 每一列有多少行-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ NSInteger row = 0; switch (component) { case 0: row = self.pickerNameArray.count; break; case 1: row = self.pickerPlaceArray.count; break; } return row;} // 利用系统自带的格式。-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ if (component == 0) { return self.pickerNameArray[row]; } else { return self.pickerPlaceArray[row]; } }// 自定义格式-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ UILabel *contentLabel = nil; if (component == 0) { contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)]; contentLabel.textAlignment = NSTextAlignmentCenter; contentLabel.text = self.pickerNameArray[row]; contentLabel.font = [UIFont systemFontOfSize:14]; contentLabel.backgroundColor = [UIColor cyanColor]; }else { contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 180, 30)]; contentLabel.text = self.pickerPlaceArray[row]; contentLabel.textAlignment = NSTextAlignmentCenter; contentLabel.font = [UIFont systemFontOfSize:14]; contentLabel.backgroundColor = [UIColor darkGrayColor]; } return contentLabel;}-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ switch (component) { case 0: { self.str1 = self.pickerNameArray[row]; self.pickerPlaceArray = self.typeDictionary[self.str1]; if (self.pickerPlaceArray.count >1) { self.selectStr = [NSString stringWithFormat:@"%@, %@",self.str1,[self.pickerPlaceArray objectAtIndex:b]]; } else { self.selectStr = self.str1; } // 更新视图 [pickerView reloadComponent:1]; [pickerView selectRow:b inComponent:1 animated:YES]; } break; case 1 : { self.str2 = self.pickerPlaceArray[row]; self.selectStr = [NSString stringWithFormat:@"%@ ,%@",self.str1,self.str2]; break; } break; } self.lblSelect.text = self.selectStr;} // 设置每一列的行高-(CGFloat) pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ return 40;}// 设置每一列的宽度-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ CGFloat width = 0.0; if (component == 0) { width = 100; } else width = 180; return width;}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end运行结果如下:
0 0
- iPhone开发UI组件UIDatePickerView的预习
- iphone开发UI组件之UIDatePicker日期选择组件的预习
- iphone开发之UIImagePickerController组件的预习————用于照相和打开图库
- iPhone开发之工具栏控制器UITabBarController的预习
- iPhone开发之导航控制器的预习——UINavigationController导航控制器的学习
- iPhone开发之手势的预习—— UITapGestureRecognizer pinch捏合 longPress长按 rotation旋转
- iPhone UI开发的一些注意事项
- iPhone UI开发建议
- iphone UI 开发教程
- 基于原生HTML的UI组件开发
- IPhone UI开发常用代码
- 11个iPhone和iPad开发的UI包
- iphone开发,ui界面和后台线程的运行机制
- UIDatePickerView的完美使用 日期选取器
- [iOS datePickerView] UIDatePickerView 的简单用法
- iphone开发, Datepicker组件使用
- iphone开发环境组件介绍
- Android开发自定义UI组件
- CC2530设置发射功率
- iphone开发UI组件之UIDatePicker日期选择组件的预习
- Scala类型检查与转换
- web服务器nginx和apache的对比分析
- 欢迎使用CSDN-markdown编辑器
- iPhone开发UI组件UIDatePickerView的预习
- JSP学习 --- servlet的生命周期
- 运用bootstrap框架写的后台模板
- 【AHOI2012】【BZOJ2823】信号塔
- java设计6大设计原则
- 黑马程序员------Java基础-----网络编程
- iPhone开发之UITableView的小试牛刀 利用MVC模式从plist文件读取数据并字典转模型的格式实现
- testlink使用
- HDU 4609(3-idiots-FFT+dp)