iOS开发第三方键盘处理实例代码
最近项目中遇到了键盘处理通知被调用多次的情况,废了好半天时间才找到解决办法,今天就给小伙伴儿们唠唠第三方键盘处理的那些坑!
详情请看:『https://github.com/boai/bakeyboarddemo』 !
1、聊天评论框的封装
先聊聊我项目中遇到的奇葩情况吧,一个直播界面,上面播放器,下面是分段控制器5个button,5个界面,其中三个界面最下面都是评论框,所以就封装了一个评论框公用。
但是本来用的『iqkeyboardmanager』,开源键盘处理框架,可是在同一个界面有多个评论框就出现问题了。
2、先看看『iqkeyboardmanager』的使用吧:
#import "appdelegate.h" appdelegate 中添加这段代码,就可以全局不用管键盘的弹起收回了! #pragma mark - 键盘处理 - (void)completionhandleiqkeyboard { iqkeyboardmanager *manager = [iqkeyboardmanager sharedmanager]; manager.enable = yes; manager.shouldresignontouchoutside = yes; manager.shouldtoolbarusestextfieldtintcolor = yes; manager.enableautotoolbar = yes; }
3、具体解决办法
但是我项目中得复杂情况就不行了,键盘弹起异常,收回也异常,尤其是用了聊天室这种view,处理的更麻烦,不要急,看看博爱这些年踩过的坑吧:
先看看键盘处理事件吧:
- 1、首先注册通知: - (void)registnotification { [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwasshown:) name:uikeyboardwillshownotification object:nil]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwashidden:) name:uikeyboardwillhidenotification object:nil]; } - 2、再把后路想好:移除通知 - (void)removenotification { [[nsnotificationcenter defaultcenter] removeobserver:self]; } - 3、通知事件处理: /*! 键盘显示要做什么 */ - (void)keyboardwasshown:(nsnotification *)notification { nsdictionary *info = [notification userinfo]; double duration = [info[uikeyboardanimationdurationuserinfokey] doublevalue]; cgfloat curkeyboardheight = [[info objectforkey:@"uikeyboardboundsuserinfokey"] cgrectvalue].size.height; cgrect begin = [[info objectforkey:@"uikeyboardframebeginuserinfokey"] cgrectvalue]; cgrect end = [[info objectforkey:@"uikeyboardframeenduserinfokey"] cgrectvalue]; cgfloat keyboardheight; /*! 第三方键盘回调三次问题,监听仅执行最后一次 */ if(begin.size.height > 0 && (begin.origin.y - end.origin.y > 0)) { keyboardheight = curkeyboardheight; [uiview animatewithduration:duration animations:^{ cgrect viewframe = [self getcurrentviewcontroller].view.frame; viewframe.origin.y -= keyboardheight; [self getcurrentviewcontroller].view.frame = viewframe; }]; } } - (void)keyboardwashidden:(nsnotification *)notification { nsdictionary *info = [notification userinfo]; double duration = [info[uikeyboardanimationdurationuserinfokey] doublevalue]; [uiview animatewithduration:duration animations:^{ cgrect viewframe = [self getcurrentviewcontroller].view.frame; viewframe.origin.y = 0; [self getcurrentviewcontroller].view.frame = viewframe; }]; } /*! * 获取当前view的vc * * @return 获取当前view的vc */ - (uiviewcontroller *)getcurrentviewcontroller { for (uiview *view = self; view; view = view.superview) { uiresponder *nextresponder = [view nextresponder]; if ([nextresponder iskindofclass:[uiviewcontroller class]]) { return (uiviewcontroller *)nextresponder; } } return nil; }
具体情况是这样的,在测试过程中,其他界面的评论框都没问题,就直播这个vc有问题,就一步步往下找,后来发现:ios的第三方键盘会在【- (void)keyboardwasshown:(nsnotification *)notification】这个方法中来回调用多次,不止三次好像,然后就想到一个办法,
/*! 第三方键盘回调三次问题,监听仅执行最后一次 */ if(begin.size.height > 0 && (begin.origin.y - end.origin.y > 0)) { keyboardheight = curkeyboardheight; [uiview animatewithduration:duration animations:^{ cgrect viewframe = [self getcurrentviewcontroller].view.frame; viewframe.origin.y -= keyboardheight; [self getcurrentviewcontroller].view.frame = viewframe; }]; }
在这里处理这个键盘弹起事件中第一次获取键盘的高度,然后就直接把上面的view给弹上去,这样就避免了第三方键盘会来回调用多次方法,造成键盘弹起异常的问题就迎刃而解了!
4、如果这样还不能解决你的键盘问题,还有中万能方法:
平时可能遇到这种需求:点击一个按钮,弹出评论框和键盘,这时你就需要这样处理了:
1、创建一个 textfield、textfield2,把textfield位置放到屏幕外面看不到的地方,textfield 有个属性,用法如下:
self.replytextfield.inputaccessoryview = self.replytextfield2;
需要添加target 事件:
[self.replytextfield addtarget:self action:@selector(replytextfieldchanged:) forcontrolevents:uicontroleventeditingchanged];
事件方法处理:
- (void)replytextfieldchanged:(uitextfield *)textfield { nslog(@"textfieldshouldbeginediting输入内容****:%@", textfield.text); if (textfield != self.replytextfield2) { self.replytextfield2.text = textfield.text; } nslog(@"textfieldshouldbeginediting输入内容1:%@", self.replytextfield.text); nslog(@"textfieldshouldbeginediting输入内容2:%@", self.replytextfield2.text); } - (bool)textfieldshouldreturn:(uitextfield *)textfield { [self.replytextfield resignfirstresponder]; [self.replytextfield2 resignfirstresponder]; [[self getcurrentviewcontroller].view endediting:yes]; if (self.clickindexblock) { self.clickindexblock(self.replytextfield2.text); self.replytextfield.text = @""; self.replytextfield2.text = @""; } return yes; }
这样处理,不管你的键盘在哪里,输入框都会跟着你的键盘走,而且不会再出现错位,计算不准确的地方!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。