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/

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值