UIPickerView 继承了UIView 没有继承UIControl UIPickerView的时间处理由其委托对象完成
使用Component标明列 Row标明列中单个元素
numberOfCompinents:获取UIPickerView 指定列中包含的列表项的数量。该属性只读
showsSelectionIndicator: 是否显示UIPV中的选中标记(以高亮背景作为选中标记)
numberOfRowsInComponent: 获取UIPV包含的列数量
rowSizeForCompinent:获取UIPV包含的指定列中列表项的大小。该方法返回一个CGSize对象
selectRow:inComponent:animated:该方法设置选中该UIPV中指定列的特定列表项 最后一个 是否使用动画
selectRowInCompinent:返回该UIPickerView指定列中悲酸中的列表项
viewForRow:forComponent:返回该UIPV指定列的列表项所使用的UIView控件
UIDatePicker只是负责该空间的通用行为,而该空间包含多少列,格列包含多少个列表项则有UIPickerViewDataSource对象负责
开发者必须weiUIPickerView设置UIPickerViewDataSource对象,并实现两种方法:
numberOFComponentsInPickerView:该UIPickerView将通过该方法来判断应该包含多少列
pickerView:numBerOfRowsInCompont:判断指定列包含多少个列表项
UIPickerView需要控制各列额宽度,以及各列中列表项的大小和外观,或程序需要为UIPicker的选中事件提供相应,都需要为UIPickerView设置UIPVDelegate委托对象,并根据需要实现该委托对象中的如下方法
-pickerView:rowHeightForCompinent:该方法返回的CGFloatz值将作为该UIPV控件中指定列中列表项的高度
-pickerView:widthForComponent:宽
-~titleForRow;forComponent:指定列的列表项的文本标题
~viewForRow:forComponent:reusingView:返回的UIPV控件中指定列的指定列表项
UIPV-InterfaceBuilder -ShowsSelectionIndicator 是否显示UIPV中选中标记(高亮背景)
10.12.1 单列选择器
- (void)viewDidLoad {
[super viewDidLoad];
self.books=[NSArray arrayWithObjects:@"第一项",@"第二项",@"第三项", nil];
self.pickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 20, 400, 500)];
self.pickerView.dataSource=self;
self.pickerView.delegate=self;
[self.view addSubview:self.pickerView];
// Do any additional setup after loading the view, typically from a nib.
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return self.books.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//根据row参数返回books中的元素,row参数代表列表项的编号
//因此该方法表示第几个列表项,就使用books中的第几个元素
return [self.books objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
//使用一个UIAlertView来系那是用户选中的列表项
UIAlertView* alert=[[UIAlertView alloc]
initWithTitle:@"提示" message:[NSString stringWithFormat: @"你选中的图书是:%@",[self.books objectAtIndex:row]] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
}
10.12.2多项选择器
- (void)viewDidLoad {
[super viewDidLoad];
_initState=0;
self.books=[NSArray arrayWithObjects:@"诗集1",@"诗集2",@"诗集3", nil];
self.haiMingWei=[NSArray arrayWithObjects:@"这里的黎明静悄悄",@"老人与海", nil];
self.shaShiBiYa=[NSArray arrayWithObjects:@"十四行诗集",@"慕克白",@"哈姆莱特",@"罗密欧与朱丽叶", nil];
// self.dict=[NSDictionary dictionaryWithObjectsAndKeys:self.books,@"叶慈",
// self.haiMingWei,@"海明威",
// self.shaShiBiYa,@"罗密欧与朱丽叶",nil];
self.author=[NSArray arrayWithObjects:@"海明威",@"莎士比亚",@"叶慈", nil];
self.pickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 20, 400, 500)];
self.pickerView.dataSource=self;
self.pickerView.delegate=self;
self.i=0;
[self.view addSubview:self.pickerView];
// Do any additional setup after loading the view, typically from a nib.
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component==0) {
return self.author.count;
}
if ([self.pickerView selectedRowInComponent:0]==0) {
return self.haiMingWei.count;
}else if ([self.pickerView selectedRowInComponent:0]==1) {
return self.shaShiBiYa.count;
}else
return self.books.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//根据row参数返回books中的元素,row参数代表列表项的编号
//因此该方法表示第几个列表项,就使用books中的第几个元素
if (component==0) {
return self.author[row];
}
switch ([self.pickerView selectedRowInComponent:0]) {
case 0:
return self.haiMingWei[row];
break;
case 1:
return self.shaShiBiYa[row];
break;
case 2:
return self.books[row];
break;
default:
return self.haiMingWei[row];
break;
}
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
//使用一个UIAlertView来系那是用户选中的列表项
if (component==0) {
[self.pickerView selectRow:row inComponent:component animated:YES];
[self.pickerView reloadComponent:1];
}
}
实现了单列巡回滚动的UIPickerView
.h——————————————————————
@property (nonatomic) UIPickerView* pickerView;
@property (nonatomic) UIImage* lose;
@property (nonatomic) UIImage* win;
@property (nonatomic) NSMutableArray* images;
@property (nonatomic) UIImageView* view;
@property (nonatomic) UIButton* start;
@property (nonatomic) UIImageView* image;
@property (nonatomic,strong) NSArray* imagesTemp;//原图片序列
@property (nonatomic,strong) NSMutableArray* comArr;//控制每一列的图片,每一序列是picker一列的所有图片集合
.m——————————————————————
- (void)viewDidLoad {
[super viewDidLoad];
UIImage* dog=[UIImage imageNamed:@"./images/dog.png"];
UIImage* duck=[UIImage imageNamed:@"./images/duck.png"];
UIImage* elephant=[UIImage imageNamed:@"./images/elephant.png"];
UIImage* frog=[UIImage imageNamed:@"./images/frog.png"];
UIImage* mouse=[UIImage imageNamed:@"./images/mouse.png"];
UIImage* rabbit=[UIImage imageNamed:@"./images/rabbit.png"];
self.lose=[UIImage imageNamed:@"./images/lose.jpg"];
self.win=[UIImage imageNamed:@"./images/win.gif"];
self.images=[NSMutableArray arrayWithObjects:dog,duck,
elephant,frog,mouse,rabbit, nil];
self.imagesTemp=[NSMutableArray arrayWithArray:self.images];
self.comArr=[NSMutableArray arrayWithObjects:
self.imagesTemp,
self.imagesTemp,
self.imagesTemp,
self.imagesTemp,
self.imagesTemp,
self.imagesTemp
,nil];
//加载输赢结果的图标显示
self.image=[[UIImageView alloc]initWithFrame:CGRectMake(190, 330, 40, 40)];
[self.view addSubview:self.image];
//加载主体
self.pickerView=[[UIPickerView alloc]initWithFrame:
CGRectMake(20, 20, 380, 200)];
self.pickerView.dataSource=self;
self.pickerView.delegate=self;
self.pickerView.userInteractionEnabled=NO;
[self.view addSubview:self.pickerView];
//加载开始按钮
self.start=[UIButton buttonWithType:UIButtonTypeRoundedRect];
self.start.frame=CGRectMake(200, 400, 40, 50);
[self.start setTitle:@"开始" forState:UIControlStateNormal];
[self.start addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.start];
// Do any additional setup after loading the view, typically from a nib.
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 5;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return self.images.count;
}
#define kImageTag 1
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
if (view.tag!=kImageTag) {
view=[[UIImageView alloc]initWithImage:[self.comArr[component] objectAtIndex:row]];
view.tag=kImageTag;
view.userInteractionEnabled=NO;
}
return view;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 40;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
return 40;
}
-(void)clicked:(id)sender{
self.start.enabled=NO;
self.image.image=nil;
NSMutableDictionary* result=[[NSMutableDictionary alloc]
initWithCapacity:6];
NSURL* winSoundUrl=[[NSBundle mainBundle]
URLForResource:@"crunch" withExtension:@"wav"];
for (int i=0; i<5;i++) {
NSUInteger selectedVal=arc4random()%self.images.count;
//为了选择器列之间不会互相影响,新建一个数组
NSMutableArray* arr=[NSMutableArray arrayWithArray:self.imagesTemp];
for (int j=0; j
[arr setObject: self.imagesTemp[(selectedVal+j)%self.images.count]
atIndexedSubscript:(j+3)%self.imagesTemp.count];
}
//将重新归序的数组放入到第i列的中,重新加载
[self.comArr setObject:arr atIndexedSubscript:i];
[self.pickerView reloadComponent:i];
[self.pickerView selectRow:3 inComponent:i animated:YES];
//在result中已经为该随机数记录了出现次数
if ([result objectForKey:[NSNumber numberWithInt:selectedVal]]) {
//获取result中该随机数的出现次数
NSUInteger newCount=[[result objectForKey:
[NSNumber numberWithInt:selectedVal] ] integerValue];
//将result中该随机数的出现次数+1
[result setObject:[NSNumber numberWithInt:(newCount+1)] forKey:[NSNumber numberWithInt:selectedVal]];
}else
{
//使用result记录该随机数的出现次数为1
[result setObject:[NSNumber numberWithInt:1] forKey:[NSNumber numberWithInt:selectedVal]];
}
//使用该变量记录随机数的最大出现次数
NSUInteger maxOccurs=1;
for (NSNumber* num in [result allKeys] ) {
//只要任何随机数的出现次数大于maxOccurs
if ([[result objectForKey:num]integerValue]>maxOccurs) {
//使用maxOccurs保存该随机数的出现次数
maxOccurs=[[result objectForKey:num] integerValue];
}
}
//如果某个随机数的出现次数大于或等于3(既是界面出现了三个相同图案
if (maxOccurs>=3) {
//如果赢了延迟0.5秒执行showWin方法,显示结果
[self performSelector:@selector(showWin) withObject:nil afterDelay:0.5];
}else{
//
[self performSelector:@selector(showLose) withObject:nil afterDelay:0.5];
}
}
}
-(void)showWin{
self.image.image=self.win;
self.start.enabled=YES;
}
-(void)showLose{
self.image.image=self.lose;
self.start.enabled=YES;
}
//当运行后,图片不显示,则可能初始化加载的图片为空,可能图片路径有问题,或者没有加入到项目中
加入项目
1.使用addfileTo xCode左侧右键
2.在target-BuildPhases-CompileSources 中加入图片文件