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.
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 ;)
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.
精彩评论