iphone底部出现时间选择器

我们用iphone控件实现form表单时,有时需要添加时间控件,一般的做法是添加按钮,点击按钮从底部出现时间选择器。如下图:

image

实现代码:

#import "iphone_datapicketViewController.h"
@implementation iphone_datapicketViewController
@synthesize myDatePicker,myView;
- (void)viewDidLoad {
    [super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
    self.myDatePicker =nil;
    self.myView=nil;
}
- (void)dealloc {
    [self.myDatePicker release];
    [self.myView release];
    [super dealloc];
}
-(IBAction)onClickButton:(id)sender
{
    [self showDatePick];
}
-(IBAction)onClickCloseButton:(id)sender
{
    [self dissDatePick];
}
-(void)showDatePick
{
    if (self.myView.superview == nil)
    {
        [self.view.window addSubview: self.myView];
    }   
    CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
    CGSize pickerSize = [self.myView sizeThatFits:CGSizeZero];
    CGRect startRect = CGRectMake(0.0,
                                  screenRect.origin.y + screenRect.size.height,
                                  pickerSize.width, pickerSize.height);
    self.myView.frame. = startRect;
    CGRect pickerRect = CGRectMake(0.0,
                                   screenRect.origin.y + screenRect.size.height – pickerSize.height,
                                   pickerSize.width,
                                   pickerSize.height);
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelegate:self];
    self.myView.frame. = pickerRect;
    CGRect newFrame. = self.view.frame;
    newFrame.size.height -= self.myView.frame.size.height;
    self.view.frame. = newFrame;
    [UIView commitAnimations];
}
-(void)dissDatePick
{
    CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
    CGRect endFrame. = self.myView.frame;
    endFrame.origin.y = screenRect.origin.y + screenRect.size.height;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelegate:self];
    self.myView.frame. = endFrame;
    [UIView commitAnimations];
    CGRect newFrame. = self.view.frame;
    newFrame.size.height += self.myView.frame.size.height;
    self.view.frame. = newFrame;
}
@end

还有利用ib创建控件和相连。

源代码:http://easymorse-iphone.googlecode.com/svn/trunk/iphone.datapicket/

共有0个回答