本文主要是介绍iPhone开发UI组件UIDatePickerView的预习,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
新建工程如下:
编辑控制器的.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];// buttonself.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;}elsewidth = 180;return width;
}
- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end
运行结果如下:
这篇关于iPhone开发UI组件UIDatePickerView的预习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!