IOS中的UIPickerView和UIDatePicker

.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值