.m文件
最终的实现效果如下图所示,可以实现对每一行的拾取,还有选取时间进行倒计时的效果。
#import "ViewController.h"
@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
{
UIPickerView * _pickerView;
UIDatePicker * _datePicker;
UILabel * _downLabel;
NSTimer * _timer;
NSTimeInterval _countTime;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(10, 90, 355, 120)];
_pickerView.dataSource = self;
_pickerView.delegate = self;
[self.view addSubview:_pickerView];
_datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, 300, 355, 120)];
//日期拾取器的日期模式
_datePicker.datePickerMode = UIDatePickerModeCountDownTimer;
//日期拾取器的最大拾取日期
_datePicker.maximumDate = [NSDate date];
//倒计时的秒数
// _datePicker.countDownDuration
[self.view addSubview:_datePicker];
UIButton * sureButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 120, 40)];
sureButton.center = CGPointMake(CGRectGetWidth(self.view.frame)/2, 600);
sureButton.backgroundColor = [UIColor blackColor];
[sureButton setTitle:@"开始" forState:UIControlStateNormal];
[sureButton addTarget:self action:@selector(pressedSureButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sureButton];
_downLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 260, 30)];
_downLabel.center = CGPointMake(CGRectGetMidX(self.view.frame), 265);
_downLabel.text = @"倒计时";
_downLabel.textAlignment = NSTextAlignmentCenter;
_downLabel.font = [UIFont systemFontOfSize:25];
[self.view addSubview:_downLabel];
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(pressedTimer) userInfo:nil repeats:YES];
_timer.fireDate = [NSDate distantFuture];//(关闭定时器)将定时器设置到未来某个时间
}
- (void)pressedSureButton
{
_downLabel.text = [NSString stringWithFormat:@"还剩%.f秒",_datePicker.countDownDuration];
_timer.fireDate = [NSDate date];
// _countTime = _datePicker.countDownDuration;
}
//- (void)pressedTimer
//{
// _countTime --;
// if (_countTime <= 0) {
// _downLabel.text = @"时间到";
// _timer.fireDate = [NSDate distantFuture];
// }else {
// _downLabel.text = [NSString stringWithFormat:@"还剩%.f秒",_countTime];
// }
//}
- (void)pressedTimer
{
static double i = 0;
_downLabel.text = [NSString stringWithFormat:@"还剩%.f秒",_datePicker.countDownDuration - i];
if (_datePicker.countDownDuration - i == 0) {
_downLabel.text = @"时间到";
_timer.fireDate = [NSDate distantFuture];
}
i++;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//滚动到第几列第几行
[_pickerView selectRow:3 inComponent:0 animated:YES];
[_pickerView selectRow:5 inComponent:1 animated:YES];
[_pickerView selectRow:8 inComponent:2 animated:YES];
}
#pragma mark - <UIPickerViewDataSource>
//每一列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
switch (component) {
case 0:
return 10;
break;
case 1:
return 15;
break;
case 2:
return 20;
break;
default:
break;
}
return 12;
}
//有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
#pragma mark - <UIPickerViewDelegate>
//配置每行的标题
//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
//{
//
// return [NSString stringWithFormat:@"第%ld列第%ld行",component,row];
//}
//配置列宽
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
return 100;
}
//配置行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 30;
}
//配置每一行的视图
//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
//{
//
// UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 90, 30)];
// label.textAlignment = NSTextAlignmentCenter;
// label.font = [UIFont systemFontOfSize:15];
//
// label.text = [NSString stringWithFormat:@"第%ld列第%ld行",component,row];
// return label;
//}
//配置文字属性
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSShadow * shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor grayColor];
shadow.shadowOffset = CGSizeMake(5,2);
shadow.shadowBlurRadius = 5;//阴影的模糊
NSAttributedString * string = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"第%ld列第%ld行",component,row] attributes:@{NSForegroundColorAttributeName:[UIColor cyanColor],NSShadowAttributeName:shadow}];
return string;
}
//选中了第几列第几行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(@"第%ld第%ld",row,component);
}
@end