国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

iOS開(kāi)發(fā)第三方鍵盤(pán)處理實(shí)例代碼

2019-10-21 18:45:59
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

最近項(xiàng)目中遇到了鍵盤(pán)處理通知被調(diào)用多次的情況,廢了好半天時(shí)間才找到解決辦法,今天就給小伙伴兒們嘮嘮第三方鍵盤(pán)處理的那些坑!

詳情請(qǐng)看:『https://github.com/boai/BAKeyboardDemo』 !

1、聊天評(píng)論框的封裝

先聊聊我項(xiàng)目中遇到的奇葩情況吧,一個(gè)直播界面,上面播放器,下面是分段控制器5個(gè)button,5個(gè)界面,其中三個(gè)界面最下面都是評(píng)論框,所以就封裝了一個(gè)評(píng)論框公用。
但是本來(lái)用的『IQKeyboardManager』,開(kāi)源鍵盤(pán)處理框架,可是在同一個(gè)界面有多個(gè)評(píng)論框就出現(xiàn)問(wèn)題了。

2、先看看『IQKeyboardManager』的使用吧:

#import "AppDelegate.h"AppDelegate 中添加這段代碼,就可以全局不用管鍵盤(pán)的彈起收回了!#pragma mark - 鍵盤(pán)處理- (void)completionHandleIQKeyboard{ IQKeyboardManager *manager = [IQKeyboardManager sharedManager]; manager.enable = YES; manager.shouldResignOnTouchOutside = YES; manager.shouldToolbarUsesTextFieldTintColor = YES; manager.enableAutoToolbar = YES;}

3、具體解決辦法

但是我項(xiàng)目中得復(fù)雜情況就不行了,鍵盤(pán)彈起異常,收回也異常,尤其是用了聊天室這種view,處理的更麻煩,不要急,看看博愛(ài)這些年踩過(guò)的坑吧:

先看看鍵盤(pán)處理事件吧:

- 1、首先注冊(cè)通知:- (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、通知事件處理:/*! 鍵盤(pán)顯示要做什么 */- (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; /*! 第三方鍵盤(pán)回調(diào)三次問(wèn)題,監(jiān)聽(tīng)僅執(zhí)行最后一次 */ 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; }];}/*!* 獲取當(dāng)前View的VC** @return 獲取當(dāng)前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;}

具體情況是這樣的,在測(cè)試過(guò)程中,其他界面的評(píng)論框都沒(méi)問(wèn)題,就直播這個(gè)VC有問(wèn)題,就一步步往下找,后來(lái)發(fā)現(xiàn):iOS的第三方鍵盤(pán)會(huì)在【- (void)keyboardWasShown:(NSNotification *)notification】這個(gè)方法中來(lái)回調(diào)用多次,不止三次好像,然后就想到一個(gè)辦法,

/*! 第三方鍵盤(pán)回調(diào)三次問(wèn)題,監(jiān)聽(tīng)僅執(zhí)行最后一次 */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; }];}

在這里處理這個(gè)鍵盤(pán)彈起事件中第一次獲取鍵盤(pán)的高度,然后就直接把上面的view給彈上去,這樣就避免了第三方鍵盤(pán)會(huì)來(lái)回調(diào)用多次方法,造成鍵盤(pán)彈起異常的問(wèn)題就迎刃而解了!

4、如果這樣還不能解決你的鍵盤(pán)問(wèn)題,還有中萬(wàn)能方法:

平時(shí)可能遇到這種需求:點(diǎn)擊一個(gè)按鈕,彈出評(píng)論框和鍵盤(pán),這時(shí)你就需要這樣處理了:

1、創(chuàng)建一個(gè)  TextField、TextField2,把TextField位置放到屏幕外面看不到的地方,TextField 有個(gè)屬性,用法如下:

self.replyTextField.inputAccessoryView = self.replyTextField2;

需要添加target 事件:

 

復(fù)制代碼 代碼如下:

[self.replyTextField addTarget:self action:@selector(replyTextFieldChanged:) forControlEvents:UIControlEventEditingChanged];

 

事件方法處理:

- (void)replyTextFieldChanged:(UITextField *)textField{ NSLog(@"textFieldShouldBeginEditing輸入內(nèi)容****:%@", textField.text); if (textField != self.replyTextField2) {  self.replyTextField2.text = textField.text; } NSLog(@"textFieldShouldBeginEditing輸入內(nèi)容1:%@", self.replyTextField.text); NSLog(@"textFieldShouldBeginEditing輸入內(nèi)容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;}

這樣處理,不管你的鍵盤(pán)在哪里,輸入框都會(huì)跟著你的鍵盤(pán)走,而且不會(huì)再出現(xiàn)錯(cuò)位,計(jì)算不準(zhǔn)確的地方!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到IOS開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 天柱县| 锦州市| 蒙阴县| 如皋市| 晴隆县| 隆安县| 灵山县| 南平市| 昌都县| 和顺县| 北海市| 陵水| 福建省| 老河口市| 林甸县| 平江县| 贵港市| 弋阳县| 蓬莱市| 大安市| 龙泉市| 东台市| 建平县| 辽中县| 新民市| 大竹县| 武强县| 尼玛县| 仪征市| 三亚市| 道真| 镇平县| 茂名市| 宝鸡市| 化隆| 安图县| 大关县| 大冶市| 民和| 驻马店市| 容城县|