iPhone實(shí)例 底部出現(xiàn)時(shí)間選擇器
iPhone實(shí)例 底部出現(xiàn)時(shí)間選擇器是本文要介紹的內(nèi)容,我們用iphone控件實(shí)現(xiàn)form表單時(shí),有時(shí)需要添加時(shí)間控件,一般的做法是添加按鈕,點(diǎn)擊按鈕從底部出現(xiàn)時(shí)間選擇器。如下圖:
實(shí)現(xiàn)代碼:
- #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創(chuàng)建控件和相連。
源代碼:http://easymorse-iphone.googlecode.com/svn/trunk/iphone.datapicket/
小結(jié):iPhone實(shí)例 底部出現(xiàn)時(shí)間選擇器的內(nèi)容介紹完了,希望本文對你有所幫助!