1.注册键盘通知事件

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

    // 键盘将出现事件监听
    [center addObserver:self selector:@selector(handleKeyboardWillShow:)

                   name:UIKeyboardWillShowNotification

                 object:nil];

    // 键盘将隐藏事件监听
    [center addObserver:self selector:@selector(handleKeyboardWillHide:)

                   name:UIKeyboardWillHideNotification

                 object:nil];

 

2.处理动画

// 键盘出现

- (void)handleKeyboardWillShow:(NSNotification *)paramNotification
{
    NSDictionary *userInfo = [paramNotification userInfo];

    NSValue *animationCurveObject = [userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey];

    NSValue *animationDurationObject = [userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSValue *keyboardEndRectObject = [userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];

    NSUInteger animationCurve = 0;

    double animationDuration = 0.0f;

    CGRect keyboardEndRect = CGRectMake(0,0, 0, 0);

    [animationCurveObject getValue:&animationCurve];

    [animationDurationObject getValue:&animationDuration];

    [keyboardEndRectObject getValue:&keyboardEndRect];

    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];

    CGRect intersectionOfKeyboardRectAndWindowRect = CGRectIntersection(window.frame, keyboardEndRect);

    CGFloat bottomInset = intersectionOfKeyboardRectAndWindowRect.size.height;

    [UIView beginAnimations:@"changeViewContentInset" context:NULL];

    [UIView setAnimationDuration:animationDuration];

    [UIView setAnimationCurve:(UIViewAnimationCurve) animationCurve];

    CGRect tempRect = self.contentView.frame;

    tempRect.origin.y = Main_Screen_Height - bottomInset - 260 - 20;

    self.contentView.frame = tempRect;

    [UIView commitAnimations];
}
// 键盘消失

- (void)handleKeyboardWillHide:(NSNotification *)paramNotification
{
    NSDictionary *userInfo = [paramNotification userInfo];

    NSValue *animationCurveObject =[userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey];

    NSValue *animationDurationObject = [userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSValue *keyboardEndRectObject =[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];

    NSUInteger animationCurve = 0;

    double animationDuration = 0.0f;

    CGRect keyboardEndRect = CGRectMake(0, 0, 0, 0);

    [animationCurveObject getValue:&animationCurve];

    [animationDurationObject getValue:&animationDuration];

    [keyboardEndRectObject getValue:&keyboardEndRect];

    [UIView beginAnimations:@"changeViewContentInset" context:NULL];

    [UIView setAnimationDuration:animationDuration];

    [UIView setAnimationCurve:(UIViewAnimationCurve)animationCurve];

    self.contentView.center = self.center;

    [UIView commitAnimations];
}

 

 3.当然是别忘了注销通知哦

[[NSNotificationCenter defaultCenter] removeObserver:self];

相关文章:

  • 2021-09-03
  • 2021-10-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-29
  • 2021-10-30
  • 2021-12-09
  • 2021-09-27
  • 2021-10-25
相关资源
相似解决方案