iOS键盘动画细节

栏目: IOS · 发布时间: 6年前

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

iOS键盘动画细节

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

上图

iOS键盘动画细节

动画细节代码

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

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

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

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

然后是实现的核心代码

- (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

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

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

- (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做的动画

最后 别忘记移除通知

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

总结

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

Demo点击这里下载

全文完


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Java并发编程的艺术

Java并发编程的艺术

方腾飞、魏鹏、程晓明 / 机械工业出版社 / 2015-7-1 / 59.00元

并发编程领域的扛鼎之作,作者是阿里和1号店的资深Java技术专家,对并发编程有非常深入的研究,《Java并发编程的艺术》是他们多年一线开发经验的结晶。本书的部分内容在出版早期发表在Java并发编程网和InfoQ等技术社区,得到了非常高的评价。它选取了Java并发编程中最核心的技术进行讲解,从JDK源码、JVM、CPU等多角度全面剖析和讲解了Java并发编程的框架、工具、原理和方法,对Java并发编......一起来看看 《Java并发编程的艺术》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具