类似iPhone键盘出现动画的实现


用显示键盘动画的方式来显示DatePicker

1.显示DatePicker

[plain]

  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     UITableViewCell *targetCell = [tableView cellForRowAtIndexPath:indexPath];  
  4.     self.pickerView.date = [self.dateFormatter dateFromString:targetCell.detailTextLabel.text];  
  5.       
  6.     // 查看DatePicker是否显示在屏幕上  
  7.     if (self.pickerView.superview == nil)  
  8.     {  
[plain]
  1.           // 添加选取器到屏幕上  
[plain]
  1.     [self.view.window addSubview: self.pickerView];  
  2.       
  3.     // size up the picker view to our screen and compute the start/end frame origin for our slide up animation  
  4.     //  
  5.     // 计算DatePicker初始的Frame  
  6.     CGRect screenRect = [[UIScreen mainScreen] applicationFrame];  
  7.     CGSize pickerSize = [self.pickerView sizeThatFits:CGSizeZero];  
  8.     CGRect startRect = CGRectMake(0.0,  
  9.                                   screenRect.origin.y + screenRect.size.height,  
  10.                                   pickerSize.width, pickerSize.height);  
  11.     self.pickerView.frame = startRect;  
  12.       
  13.     // compute the end frame  
  14.     CGRect pickerRect = CGRectMake(0.0,  
  15.                                    screenRect.origin.y + screenRect.size.height - pickerSize.height,  
  16.                                    pickerSize.width,  
  17.                                    pickerSize.height);  
  18.     // start the slide up animation  
  19.     [UIView beginAnimations:nil context:NULL];  
  20.         [UIView setAnimationDuration:0.3];  
  21.       
  22.         // we need to perform some post operations after the animation is complete  
  23.         [UIView setAnimationDelegate:self];  
  24.       
  25.         self.pickerView.frame = pickerRect;  
  26.       
  27.         // shrink the table vertical size to make room for the date picker  
  28.         CGRect newFrame = self.tableView.frame;  
  29.         newFrame.size.height -= self.pickerView.frame.size.height;  
  30.         self.tableView.frame = newFrame;  
  31.     [UIView commitAnimations];  
  32.       
  33.     // add the "Done" button to the nav bar  
  34.     self.navigationItem.rightBarButtonItem = self.doneButton;  
  35. }  
2.消除DatePicker

[plain]

  1. - (IBAction)doneAction:(id)sender  
  2. {  
  3.     CGRect screenRect = [[UIScreen mainScreen] applicationFrame];  
  4.     CGRect endFrame = self.pickerView.frame;  
  5.     endFrame.origin.y = screenRect.origin.y + screenRect.size.height;  
  6.       
  7.     // start the slide down animation  
  8.     [UIView beginAnimations:nil context:NULL];  
  9.         [UIView setAnimationDuration:0.3];  
  10.       
  11.         // we need to perform some post operations after the animation is complete  
  12.         [UIView setAnimationDelegate:self];  
  13.         [UIView setAnimationDidStopSelector:@selector(slideDownDidStop)];  
  14.       
  15.         self.pickerView.frame = endFrame;  
  16.     [UIView commitAnimations];  
  17.       
  18.     // grow the table back again in vertical size to make room for the date picker  
  19.     CGRect newFrame = self.tableView.frame;  
  20.     newFrame.size.height += self.pickerView.frame.size.height;  
  21.     self.tableView.frame = newFrame;  
  22.       
  23.     // remove the "Done" button in the nav bar  
  24.     self.navigationItem.rightBarButtonItem = nil;  
  25.       
  26.     // deselect the current table row  
  27.     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];  
  28.     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];  
  29. }  
3.从父视图移除

[plain]

  1. - (void)slideDownDidStop  
  2. {  
  3.     // the date picker has finished sliding downwards, so remove it  
  4.     [self.pickerView removeFromSuperview];  
  5. }  
参考代码DateCell

相关内容