歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 類似iPhone鍵盤出現動畫的實現

類似iPhone鍵盤出現動畫的實現

日期:2017/3/1 10:36:07   编辑:Linux編程

用顯示鍵盤動畫的方式來顯示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. // 查看DatePicker是否顯示在屏幕上
  6. if (self.pickerView.superview == nil)
  7. {
[plain]
  1.           // 添加選取器到屏幕上
[plain]
  1. [self.view.window addSubview: self.pickerView];
  2. // size up the picker view to our screen and compute the start/end frame origin for our slide up animation
  3. //
  4. // 計算DatePicker初始的Frame
  5. CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
  6. CGSize pickerSize = [self.pickerView sizeThatFits:CGSizeZero];
  7. CGRect startRect = CGRectMake(0.0,
  8. screenRect.origin.y + screenRect.size.height,
  9. pickerSize.width, pickerSize.height);
  10. self.pickerView.frame = startRect;
  11. // compute the end frame
  12. CGRect pickerRect = CGRectMake(0.0,
  13. screenRect.origin.y + screenRect.size.height - pickerSize.height,
  14. pickerSize.width,
  15. pickerSize.height);
  16. // start the slide up animation
  17. [UIView beginAnimations:nil context:NULL];
  18. [UIView setAnimationDuration:0.3];
  19. // we need to perform some post operations after the animation is complete
  20. [UIView setAnimationDelegate:self];
  21. self.pickerView.frame = pickerRect;
  22. // shrink the table vertical size to make room for the date picker
  23. CGRect newFrame = self.tableView.frame;
  24. newFrame.size.height -= self.pickerView.frame.size.height;
  25. self.tableView.frame = newFrame;
  26. [UIView commitAnimations];
  27. // add the "Done" button to the nav bar
  28. self.navigationItem.rightBarButtonItem = self.doneButton;
  29. }
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. // start the slide down animation
  7. [UIView beginAnimations:nil context:NULL];
  8. [UIView setAnimationDuration:0.3];
  9. // we need to perform some post operations after the animation is complete
  10. [UIView setAnimationDelegate:self];
  11. [UIView setAnimationDidStopSelector:@selector(slideDownDidStop)];
  12. self.pickerView.frame = endFrame;
  13. [UIView commitAnimations];
  14. // grow the table back again in vertical size to make room for the date picker
  15. CGRect newFrame = self.tableView.frame;
  16. newFrame.size.height += self.pickerView.frame.size.height;
  17. self.tableView.frame = newFrame;
  18. // remove the "Done" button in the nav bar
  19. self.navigationItem.rightBarButtonItem = nil;
  20. // deselect the current table row
  21. NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
  22. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  23. }
3.從父視圖移除

[plain]

  1. - (void)slideDownDidStop
  2. {
  3. // the date picker has finished sliding downwards, so remove it
  4. [self.pickerView removeFromSuperview];
  5. }
參考代碼DateCell
Copyright © Linux教程網 All Rights Reserved