主页 iOS键盘动画细节
Post
Cancel

iOS键盘动画细节

前言

很久没写技术文章里,本篇记录了一下一个键盘弹出的小细节动画,像微信一样流程

上图

动画细节代码

细节核心主要是通知中的一些key

  • 动画时长
  • 动画的出现方式

下面的通知是接收 键盘将要出现的通知UIKeyboardWillShowNotification

1
2
3
4
[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didReceiveKeyboardShowNotification:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

然后是实现的核心代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- (void)didReceiveKeyboardShowNotification:(NSNotification *)noti {
    NSDictionary *userInfo = noti.userInfo;
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;
    CGRect keyboardFrame;
    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
    
    UIViewAnimationOptions animationOptions = animationCurve << 16;
    
    self.bottomConstrains.offset = -CGRectGetHeight(keyboardFrame);
    [UIView animateWithDuration:animationDuration delay:0. options:animationOptions animations:^{
        [self.view setNeedsUpdateConstraints];
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}

self.bottomConstrains.offset = -CGRectGetHeight(keyboardFrame); 是我写的约束 详细请参考demo

键盘消失也是一样的 UIKeyboardWillHideNotification 接收这个key

1
2
3
4
[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didReceiveKeyboardHideNotification:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];

消失的时候 把约束偏移量设置0即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- (void)didReceiveKeyboardHideNotification:(NSNotification *)noti {
    NSDictionary *userInfo = noti.userInfo;
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;
    CGRect keyboardFrame;
    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
    
    UIViewAnimationOptions animationOptions = animationCurve << 16;
    self.bottomConstrains.offset = 0;
    [UIView animateWithDuration:animationDuration delay:0. options:animationOptions animations:^{
        [self.view setNeedsUpdateConstraints];
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}

self.bottomConstrains.offset = 0; //设置偏移量会原来位置

利用Masonry做的动画

最后 别忘记移除通知

1
2
3
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

总结

键盘弹出这个微小的细节 很容易被大家忽视,写这篇文章是为了记录知识和技巧,希望各位多多指教

Demo点击这里下载

全文完

该博客文章由作者通过 CC BY 4.0 进行授权。

船与灯塔

iOS扩大UIButton的点击的响应范围