时间选择器(UIDatePicker)和自定义选择器(UIPickView)

首先看一个时间选择器
本时间选择器是建立在弹出视图上的,也可以在普通视图上显示。
定义一个弹出框(具体的弹出框功能可以在我的另一篇文章可见):

//本方法是一个按钮的点击事件
-(void)customTime{
    UIAlertController *alert;
    if (!alert) {
        alert = [UIAlertController alertControllerWithTitle:@"选择时间" message:@"\n\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleActionSheet];//初始化一个标题为“选择时间”,风格是ActionSheet的UIAlertController,其中"\n"是为了给DatePicker腾出空间
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            //点击确定按钮的事件处理
        }];

        UIDatePicker *datePicker = [[UIDatePicker alloc] init];//初始化一个UIDatePicker
        [alert.view addSubview:datePicker];//将datePicker添加到UIAlertController实例中
        [alert addAction:cancel];//将确定按钮添加到UIAlertController实例中
    }
    [self presentViewController:alert animated:YES completion:^{
    }];//通过模态视图模式显示UIAlertController,相当于UIACtionSheet的show方法
}

效果图如下:

屏幕快照 2016-07-18 15.16.05.png

下面介绍一下,自定义一个选择器的步骤:

-(void)customTime{
    UIAlertController *alert;
    UIPickerView *timePicker;
    if (!alert) {
        alert = [UIAlertController alertControllerWithTitle:@"选择时间" message:@"\n\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleActionSheet];//初始化一个标题为“选择时间”,风格是ActionSheet的UIAlertController,其中"\n"是为了给DatePicker腾出空间
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            //点击确定按钮的事件处理
        }];

        /*
          此处不同,其他与上面时间选择器一样
        */
        
        //初始化选择器,并设置数据源和代理
        for (int i=1; i<=60; i++) {
             [_timeArr addObject:[[NSNumber alloc] initWithInt:i]];
        }
        timePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(30, 10, 300, 200)];
        timePicker.delegate = self;
        timePicker.dataSource = self;
        [timePicker selectRow:29 inComponent:0 animated:NO];

        //将自定义选择器添加在视图上
        [alert.view addSubview:timePicker];//将datePicker添加到UIAlertController实例中
        [alert addAction:cancel];//将确定按钮添加到UIAlertController实例中
    }
    [self presentViewController:alert animated:YES completion:^{
    }];//通过模态视图模式显示UIAlertController,相当于UIACtionSheet的show方法
}

选择器的代理方法

#pragma mark - UIPicker Delegate
//选择器分为几块
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
//选择器有多少行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [_timeArr count];
}
//每一行显示的内容
-(UIView*)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
    timeLabel.text = [[NSString alloc] initWithFormat:@"%@ 分钟",[_timeArr objectAtIndex:row]];
    timeLabel.textAlignment = NSTextAlignmentCenter;
    return timeLabel;
}

效果图如下

屏幕快照 2016-07-18 15.26.14.png

如果想将选择器放在手机中央,将弹出框的风格改UIAlertControllerStyleAlert(默认)的即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: WPF是一种强大的UI框架,它提供了许多控件,其中包括时间选择器控件。但是,由于在特定的应用程序中,需要自定义时间选择器以满足一些个性化需求。在这种情况下,可以使用WPF自定义控件来创建自己的时间选择器控件。 首先,在WPF中创建时间选择器控件,需要使用Calendar控件和TimePicker控件。Calendar控件用于显示日期,而TimePicker控件用于选择时间时间选择器控件的主体是StackPanel控件。在StackPanel控件中添加了两个控件Calendar和TimePicker,以实现时间选择器的基本功能。 然后,需要在时间选择器控件中定义一些附加属性,例如:选定日期、选定时间等等,以实现一些高级功能。 最后,为时间选择器控件添加样式,并实现一些触发器和动画效果,以使其外观和功能与应用程序的主题相匹配。 实现WPF自定义时间选择器可能需要一些时间和经验,但对于需要一个不寻常的时间选择器的应用程序来说,是值得的。 这样的时间选择器是用户友好的,具有很好的设计和功能,并且以C#编写,可以很容易地与WPF应用程序集成。 ### 回答2: WPF自定义时间选择器是一种功能强大、灵活性高的工具,它可以根据需求自行设计不同的选择器,可以实现小时、分钟、秒数的选择等多种功能。 首先,我们需要使用WPF自带的DatePicker控件和TimePicker控件来实现时间选择器。接下来,我们可以自定义控件的样式和模板,使其更符合我们的设计需求。 在自定义控件的样式时,我们需要设置控件的各个属性,比如控件的边框、背景、字体等。同时,我们可以通过设置样式来调整控件的布局和显示效果。 在时间选择器的实现中,需要涉及到一些比较复杂的计算,比如计算时间的差值、时间的格式转换等。我们可以使用C#中的DateTime类和TimeSpan类来实现这些功能。 最后对于自定义时间选择器的控件事件,需要自定义一些控件事件,使其更加符合我们的设计需求。比如增加或减少系统时钟里的时间。 总而言之,实现WPF自定义时间选择器需要对WPF控件、样式、模板、计算和控件事件等各个方面有深入的了解。只有掌握了这些知识,才能够设计出优秀的时间选择器,满足用户的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值