iOS开发中的键盘显示和隐藏

应用中,跟用户交互功能必不可少!
处理键盘的显示/隐藏可能会比较苦恼,尤其在iOS8出来之后,开放了键盘接口,允许使用第三方输入法,从而导致了键盘事件处理的错综复杂。比如说,像百度输入法,键盘右上角有个隐藏按钮,回车换行键可能位置与系统自带的键盘也有差别。不想安卓手机,人家自带返回键,免去了很多逻辑,而在iphone上,可能通过抛出第一响应者或者通过观察者模式发送隐藏键盘的通知来实现对键盘的隐藏。今天简单介绍一下使用系统键盘时经常遇到的几种情况。
我们常常抱怨这么几种情况:
1.键盘显示时,遮盖了原来的输入框或者不该遮盖的内容。如何让其他控件不被键盘遮盖?
2.键盘显示的动画与控件的位置移动不一致,导致动画不连贯。
3.相反的,键盘隐藏时,其他控件该如何移动到原来的位置?
等等。

功能:控件根据键盘的显示/隐藏进行重新定位。
贴代码:

1.为键盘的显示/隐藏事件,注册通知。

我们知道:当焦点到可输入的控件上(一般指 UITextField)时,键盘会自动显示,并且触发 UIKeyboardWillShowNotification通知;当焦点离开可输入的控件时,键盘会自动隐藏,并且触发 UIKeyboardWillHideNotification通知。所以,要注册这两种通知事件,在键盘显示/隐藏时,做我们“需要做的操作”。

[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotificationobject:nil];

    [[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotificationobject:nil];


2.键盘的显示事件的回调函数。

- (void)keyboardWillShow:(NSNotification *) notification {

    float animationDuration = [[[notification userInfovalueForKey:UIKeyboardAnimationDurationUserInfoKeyfloatValue];

    CGFloat height = [[[notification userInfo]objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height;

    

    CGRect bottomBarFrame = self.mToolBar.frame;

    

    [UIView beginAnimations:@"bottomBarUp" context:nil];

    [UIView setAnimationDuration: animationDuration];

    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    bottomBarFrame.origin.y = self.view.bounds.size.height - 44 - height;

    [self.mToolBar setFrame:bottomBarFrame]; 

    [UIView commitAnimations];

    

}


这个方法中,我学到了很多东西。

首先,当事件回调时,我收到了,[notification userInfo]返回的许多有用的数据。

 

    UIKeyboardAnimationCurveUserInfoKey = 0;

    UIKeyboardAnimationDurationUserInfoKey = "0.25";

    UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";

    UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 588}";

    UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 372}";

    UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";

    UIKeyboardFrameChangedByUserInteraction = 0;

    UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 264}, {320, 216}}";


根据这些,我能够精确的控制,关于键盘显示时的各种参数,并进行操作。让键盘显示时,让控件也进行相应的移动:[self.mToolBar setFrame:bottomBarFrame]; 


其次,关于动画的一些知识。简单动画的写法;而且,了解了一些方法的使用:

setAnimationDuration 方法:定义动画的时间。

setAnimationCurve 方法:定义动画的轨迹(动画如何执行)


 

typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {

    UIViewAnimationCurveEaseInOut,         // 开始时慢,中间快,结束时慢

    UIViewAnimationCurveEaseIn,            // 开始慢,然后加速

    UIViewAnimationCurveEaseOut,           // 逐渐减速

    UIViewAnimationCurveLinear             // 匀速

};




3.键盘的隐藏事件的回调函数。


- (void)keyboardWillHide:(NSNotification *) notification {

    float animationDuration = [[[notification userInfovalueForKey:UIKeyboardAnimationDurationUserInfoKeyfloatValue];

    CGFloat height = [[[notification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;

    

    CGRect bottomBarFrame = self.mToolBar.frame;

    [UIView beginAnimations:@"bottomBarDown" context:nil];

    [UIView setAnimationDuration: animationDuration];

    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    bottomBarFrame.origin.y += height;

    [self.mToolBar setFrame:bottomBarFrame]; 

    [UIView commitAnimations];

}


与键盘的显示事件的回调函数相反。 我收到了,[notification userInfo]返回的许多有用的数据。

    UIKeyboardAnimationCurveUserInfoKey = 0;

    UIKeyboardAnimationDurationUserInfoKey = "0.25";

    UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";

    UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 372}";

    UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 588}";

    UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 264}, {320, 216}}";

    UIKeyboardFrameChangedByUserInteraction = 0;

    UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";



当然,还有ARC的好处,不用手动释放,保证内存不被泄漏。

希望对你有所帮助!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值