如何使用UIDatePicker?

发布于 2024-11-01 04:22:05 字数 196 浏览 2 评论 0原文

我在 viewControler 上有一个按钮,我想调出 UIDatePicker。我想我需要将其动画化。

到目前为止,我已经完成了按钮设置,并在 IB 中的单独视图中拥有了 UIDatePicker。我认为以模态的方式提出它是最有意义的。

需要什么代码才能链接 UIDatePicker 通过单击按钮滑入视图,然后滑出?

谢谢。

I have a button on a viewControler that I want to bring up a UIDatePicker. I think I will need to animate it in/out.

So far I have the button setup and have the UIDatePicker in my IB in a separate view. I think having it come up modally would make the most sense.

What code is necessary to link the UIDatePicker to slide into view with the click of the button, then slide out?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的黑色迷你裙 2024-11-08 04:22:05

您可以省去麻烦,并将 UIDatePicker 设置为 UITextField 的“键盘”。像这样:

UIDatePicker *datePicker = [UIDatePicker new];
self.dateField.inputView = datePicker;

这样它的作用就像键盘一样,并根据需要进行动画输入和输出。您可以使用 textField 委托方法来告诉它消失。简单的方法已经够难的了。这就是我的哲学;)

You could save yourself the trouble and set your UIDatePicker as the "keyboard" for your UITextField. Like so:

UIDatePicker *datePicker = [UIDatePicker new];
self.dateField.inputView = datePicker;

This way it acts just like a keyboard and animates in and out as needed. You use the textField delegate methods to tell it to go away. The easy way is hard enough. That's my philosophy ;)

淡墨 2024-11-08 04:22:05

您想要维护对 UIDatePicker 的引用,然后可以使用 UIView 来动画显示 datePicker 的进出。我会将选择器包装在 UIView 中,以便您可以添加隐藏按钮或关联的控件。

在你的头文件中:

@interface MyControllerWithSlidingDatePicker : UIViewController{

  //If you use Interface builder, use this
  IBOutlet IDatePicker *picker; 
  //If you aren't using IB, use this
  //UIDatePicker *picker; 

}

@property (nonatomic, retain) UIDatePicker *picker;

@end

以及你的实现文件:

#import "MyControllerWithSlidingDatePicker.h"

@implementation MyControllerWithSlidingDatePicker

  - (id) init{
    self = [super init];
    if(self){
       //If you use Interface Builder, skip this part.

       UIDatePicker *dp = [[UIDatePicker alloc] init];
       //Configure your picker

       self.picker = dp;

       [dp release];

    }

    return self;  
  }

 - (IBAction)slidePickerIn{
   //Position the picker out of site
   [self.picker setFrame:CGRectMake(0,self.view.frame.size.height,self.width,self.picker.frame.size.height];

   //Add the picker to the view
   [self.view addSubview:self.picker];

   //This animation will work on iOS 4
   //For older iOS, use "beginAnimation:context"
   [UIView animateWithDuration:1.0 animations:^{

      [self.datePicker setFrame:CGRectMake0,self.view.frame.size.height-self.datePicker.frame.size.height,self.datePicker.frame.size.width,self.datePicker.frame.size.height];

   }  

 }

 - (void)dealloc{
   [picker release];
   [super dealloc];
 }

@end

要制作动画,您只需使用另一种方法将框架更改为原始的“屏幕外”框架。

You want to maintain a reference to your UIDatePicker and then you can use UIView to animate the datePicker in and out. I would wrap the picker in a UIView so you can add a hide button or associated controls.

In your header file:

@interface MyControllerWithSlidingDatePicker : UIViewController{

  //If you use Interface builder, use this
  IBOutlet IDatePicker *picker; 
  //If you aren't using IB, use this
  //UIDatePicker *picker; 

}

@property (nonatomic, retain) UIDatePicker *picker;

@end

And your implementation file:

#import "MyControllerWithSlidingDatePicker.h"

@implementation MyControllerWithSlidingDatePicker

  - (id) init{
    self = [super init];
    if(self){
       //If you use Interface Builder, skip this part.

       UIDatePicker *dp = [[UIDatePicker alloc] init];
       //Configure your picker

       self.picker = dp;

       [dp release];

    }

    return self;  
  }

 - (IBAction)slidePickerIn{
   //Position the picker out of site
   [self.picker setFrame:CGRectMake(0,self.view.frame.size.height,self.width,self.picker.frame.size.height];

   //Add the picker to the view
   [self.view addSubview:self.picker];

   //This animation will work on iOS 4
   //For older iOS, use "beginAnimation:context"
   [UIView animateWithDuration:1.0 animations:^{

      [self.datePicker setFrame:CGRectMake0,self.view.frame.size.height-self.datePicker.frame.size.height,self.datePicker.frame.size.width,self.datePicker.frame.size.height];

   }  

 }

 - (void)dealloc{
   [picker release];
   [super dealloc];
 }

@end

To animate out, you just make another method to change the frame to original "offscreen" frame.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文