BaseTableVC+KeyboardObserve.m 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //
  2. // BaseTableVC+KeyboardObserve.m
  3. // 乐销
  4. //
  5. // Created by 隋林栋 on 2017/4/26.
  6. // Copyright © 2017年 ping. All rights reserved.
  7. //
  8. #import "BaseTableVC+KeyboardObserve.h"
  9. //first responder
  10. #import "BaseVC+KeyboardObserve.h"
  11. @implementation BaseTableVC (KeyboardObserve)
  12. //重写改变方法
  13. - (void)keyboardDidChangeFrame:(NSNotification *)noti
  14. {
  15. // 键盘的frame
  16. CGRect frame = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  17. // 键盘的实时Y
  18. CGFloat keyHeight = SCREEN_HEIGHT - frame.origin.y;
  19. // 动画时间
  20. CGFloat keyDuration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey]floatValue];
  21. CGFloat yCorrect = 0;
  22. if (self.firstResponserView != nil && [self.firstResponserView isDescendantOfView:self.tableView]) {
  23. CGRect frame = [self.firstResponserView convertRect:self.firstResponserView.bounds toView:self.view];
  24. yCorrect = - ( SCREEN_HEIGHT - ( frame.origin.y + frame.size.height) - keyHeight - 120 );
  25. }else{
  26. return [super keyboardDidChangeFrame:noti];
  27. }
  28. if (yCorrect < 0) {
  29. yCorrect = 0;
  30. }
  31. // 执行动画
  32. if (keyHeight != 0) {
  33. [UIView animateWithDuration:keyDuration animations:^{
  34. [self.tableView setContentOffset:CGPointMake(self.tableView.contentOffset.x,self.tableView.contentOffset.y+yCorrect) animated:false];
  35. }completion:^(BOOL finished) {
  36. }];
  37. }
  38. }
  39. @end