iOS實現(xiàn)聊天輸入框功能
經(jīng)常使用微信聊天,沒事兒就會想輸入框的實現(xiàn)過程,所以抽空,也實現(xiàn)了一個輸入框的功能;


經(jīng)過封裝,使用就非常的簡單了,在需要的VC中,實現(xiàn)方法如下:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1.00];
self.keyView = [[DKSKeyboardView alloc] initWithFrame:CGRectMake(0, K_Height - 51, K_Width, 51)];
//設(shè)置代理方法
self.keyView.delegate = self;
[self.view addSubview:_keyView];
}
主要就是上面的添加,此時輸入框就已經(jīng)添加到當(dāng)前的VC中;稍后會講到里面的代理方法的作用;
工程結(jié)構(gòu)如下圖
主要是紅色線標(biāo)出的兩個類,結(jié)構(gòu)比較簡單
| 類名 | 作用 |
|---|---|
| DKSKeyboardView | 布局表情按鈕、更多按鈕、輸入框 |
| DKSTextView |
設(shè)置輸入行數(shù),輸入框內(nèi)容變化時改變輸入款高度 |
DKSKeyboardView.h中的代碼如下:
#import @protocol DKSKeyboardDelegate @optional //非必實現(xiàn)的方法 /** 點擊發(fā)送時輸入框內(nèi)的文案 @param textStr 文案 */ - (void)textViewContentText:(NSString *)textStr; /** 鍵盤的frame改變 */ - (void)keyboardChangeFrameWithMinY:(CGFloat)minY; @end @interface DKSKeyboardView : UIView @property (nonatomic, weak) id delegate; @end
關(guān)于上面的兩個代理方法,由于文章篇幅問題,實現(xiàn)的過程可參考demo,里面有詳細的注釋;
在DKSKeyboardView.m中,以下列出少量重要代碼,主要是改變frame
1、點擊輸入框,鍵盤出現(xiàn)
//鍵盤將要出現(xiàn)
- (void)keyboardWillShow:(NSNotification *)notification {
[self removeBottomViewFromSupview];
NSDictionary *userInfo = notification.userInfo;
CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//獲取鍵盤的高度
self.keyboardHeight = endFrame.size.height;
//鍵盤的動畫時長
CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:duration delay:0 options:[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue] animations:^{
self.frame = CGRectMake(0, endFrame.origin.y - self.backView.height - StatusNav_Height, K_Width, self.height);
[self changeTableViewFrame];
} completion:nil];
}
2、鍵盤消失
- (void)keyboardWillHide:(NSNotification *)notification {
//如果是彈出了底部視圖時
if (self.moreClick || self.emojiClick) {
return;
}
[UIView animateWithDuration:0.25 animations:^{
self.frame = CGRectMake(0, K_Height - StatusNav_Height - self.backView.height, K_Width, self.backView.height);
[self changeTableViewFrame];
}];
}
3、點擊更多按鈕
- (void)moreBtn:(UIButton *)btn {
self.emojiClick = NO; //主要是設(shè)置表情按鈕為未點擊狀態(tài)
if (self.moreClick == NO) {
self.moreClick = YES;
//回收鍵盤
[self.textView resignFirstResponder];
[self.emojiView removeFromSuperview];
self.emojiView = nil;
[self addSubview:self.moreView];
//改變更多、self的frame
[UIView animateWithDuration:0.25 animations:^{
self.moreView.frame = CGRectMake(0, self.backView.height, K_Width, bottomHeight);
self.frame = CGRectMake(0, K_Height - StatusNav_Height - self.backView.height - bottomHeight, K_Width, self.backView.height + bottomHeight);
[self changeTableViewFrame];
}];
} else { //再次點擊更多按鈕
//鍵盤彈起
[self.textView becomeFirstResponder];
}
}
4、改變輸入框大小
- (void)changeFrame:(CGFloat)height {
CGRect frame = self.textView.frame;
frame.size.height = height;
self.textView.frame = frame; //改變輸入框的frame
//當(dāng)輸入框大小改變時,改變backView的frame
self.backView.frame = CGRectMake(0, 0, K_Width, height + (viewMargin * 2));
self.frame = CGRectMake(0, K_Height - self.backView.height - self.keyboardHeight, K_Width, self.backView.height);
//改變更多按鈕、表情按鈕的位置
self.emojiBtn.frame = CGRectMake(viewMargin, self.backView.height - viewHeight - viewMargin, viewHeight, viewHeight);
self.moreBtn.frame = CGRectMake(self.textView.maxX + viewMargin, self.backView.height - viewHeight - viewMargin, viewHeight, viewHeight);
//主要是為了改變VC的view的frame
if (self.delegate && [self.delegate respondsToSelector:@selector(changeFrameWithMinY:)]) {
[self.delegate changeFrameWithMinY:self.minY];
}
}
以上就是聊天輸入框的簡單實現(xiàn),只是提供一個實現(xiàn)思路,如果在聊天界面中接入,還需要處理以下問題:
1、demo中沒有做tableViewCell的高度自適應(yīng);
2、輸入框文案較多時,tableViewCell可能會出現(xiàn)紊亂,此處沒有處理
demo中如果有任何問題,歡迎各位留言拍磚,小弟一定更正,共同學(xué)習(xí);
總結(jié)
以上所述是小編給大家介紹的iOS實現(xiàn)聊天輸入框功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
iOS的CoreAnimation開發(fā)框架中的Layer層動畫制作解析
在iOS中UIView層的屬性會映射到CoreAnimation框架的CALayer,這里我們來看一下iOS的CoreAnimation開發(fā)框架中的Layer層動畫制作解析,需要的朋友可以參考下2016-07-07
iOS中導(dǎo)航欄pop返回時出現(xiàn)黑塊問題的解決方法
在iOS開發(fā)的工作當(dāng)中,Push和Pop經(jīng)常用于界面之間的跳轉(zhuǎn)和返回。下面這篇文章主要給大家介紹了關(guān)于iOS中導(dǎo)航欄pop返回時出現(xiàn)黑塊問題的解決方法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。2017-10-10
Flutter?GetPageRoute實現(xiàn)嵌套導(dǎo)航學(xué)習(xí)
這篇文章主要為大家介紹了Flutter?GetPageRoute實現(xiàn)嵌套導(dǎo)航的示例學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
ios scrollview嵌套tableview同向滑動的示例
本篇文章主要介紹了ios scrollview嵌套tableview同向滑動的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11

