????```
import "bingeChatViewController.h"
import "bingeInputView.h"
/*
1.隱藏和顯示tabBar
2.為inputView 加分割線
3.title 好友聊天標(biāo)題
4.監(jiān)聽(tīng)鍵盤(pán)彈出 對(duì)相應(yīng)的布局做修改
5.獲取發(fā)送消息信息
*/
// 宏 自定義View高度為44
define KInputViewH 44
@interface bingeChatViewController () <UITableViewDelegate,UITableViewDataSource, UITextFieldDelegate>
//由這兩個(gè)控件組成
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) bingeInputView *inputView;
// 發(fā)送消息列表數(shù)組
@property (nonatomic,strong) NSMutableArray *chatMsgs;
@end
@implementation bingeChatViewController
// 1.
-(UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc]init];
// 代理方法
_tableView.dataSource = self;
_tableView.delegate = self;
// Xib View 的位置
_tableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - KInputViewH);
}
return _tableView;
}
// 2.
-(UIView *)inputView {
if (!_inputView) {
_inputView = [bingeInputView binge_inputView];
_inputView.textField.delegate = self;
_inputView.frame = CGRectMake(0, self.view.bounds.size.height - KInputViewH, self.view.bounds.size.width, KInputViewH);
}
return _inputView;
}
- (NSMutableArray *)chatMsgs {
if (!_chatMsgs) {
_chatMsgs = [NSMutableArray array];
}
return _chatMsgs;
}
// 3.
- (void)viewDidLoad {
[super viewDidLoad];
// 添加子控件
[self.view addSubview:self.tableView];
[self.view addSubview:self.inputView];
// 添加標(biāo)題
self.title = self.buddy.username;
// 點(diǎn)擊空白處收鍵盤(pán)
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];
[self.view addGestureRecognizer:singleTap];
// 獲取會(huì)話中的聊天記錄
[self binge_reloadChatMsgs];
//監(jiān)聽(tīng)鍵盤(pán)彈出 對(duì)相應(yīng)的布局組修改
[self binge_observerKeyboardFrameChange];
}
pragma mark - tabBar 隱藏和顯示
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// 隱藏 tabBar 控件
self.navigationController.tabBarController.tabBar.hidden = YES;
} - (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// 隱藏 tabBar 控件
self.navigationController.tabBarController.tabBar.hidden = NO;
}
pragma mark - UITableViewDataSource
-
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.chatMsgs.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"chatID"];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"chatID"];
EMMessage *msg = self.chatMsgs[indexPath.row];
NSLog(@"msg = %@",msg);
EMTextMessageBody *body = msg.messageBodies.firstObject;
cell.textLabel.text = body.text;
}
return cell; // 創(chuàng)建消息后 記得返回到cell
}
pragma mark - 收起鍵盤(pán)
// 滑動(dòng)空白處隱藏鍵盤(pán)
-
(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[self.view endEditing:YES];
}
// 點(diǎn)擊空白處收鍵盤(pán)
-(void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer {
[self.view endEditing:YES];
}
pragma mark - UITextFieldDelegate 發(fā)送消息
-
(BOOL)textFieldShouldReturn:(UITextField *)textField {
// 此處發(fā)送消息
// 3.創(chuàng)建EMChatText 對(duì)象
EMChatText *chatText = [[EMChatText alloc] initWithText:textField.text];// 2.創(chuàng)建body 對(duì)象,發(fā)現(xiàn)需要chatText
EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithChatObject:chatText];// 1.創(chuàng)建消息,發(fā)現(xiàn)body 對(duì)象
EMMessage *emsg = [[EMMessage alloc] initWithReceiver:self.buddy.username bodies:@[body]];// 0.要發(fā)送消息,發(fā)現(xiàn)需要?jiǎng)?chuàng)建消息
// 異步方法,發(fā)送一條消息
// 待發(fā)送的消息對(duì)象和發(fā)送后的消息對(duì)象是同一個(gè)對(duì)象,在發(fā)送過(guò)程中對(duì)象屬性可能會(huì)被更改
[[EaseMob sharedInstance].chatManager asyncSendMessage:emsg
progress:nil
prepare:^(EMMessage *message, EMError *error) {
// 即將發(fā)送時(shí)間的回調(diào)
}
onQueue:nil
completion:^(EMMessage *message, EMError *error) {
// 發(fā)送結(jié)束時(shí)的回調(diào)
if (!error) {
NSLog(@"發(fā)送成功");
// 清空文本輸入款
textField.text = nil;
// 發(fā)送成功之后將鍵盤(pán)退回去
[textField resignFirstResponder];
// 刷新界面
[self binge_reloadChatMsgs];
}
}
onQueue:nil];
// 發(fā)送成功之后 刷新列表 【定義一個(gè)可變數(shù)組】
return YES;
}
pragma mark - 私有方法
-
(void)binge_reloadChatMsgs {
// 首先刷新的時(shí)候要移除已有的對(duì)象
[self.chatMsgs removeAllObjects];// 拿到當(dāng)前的會(huì)話對(duì)象 一個(gè)傳username 另一個(gè)傳聊天樣式
/*
@brief 會(huì)話類型
@constant eConversationTypeChat 單聊會(huì)話
@constant eConversationTypeGroupChat 群聊會(huì)話
@constant eConversationTypeChatRoom 聊天室會(huì)話
*/
EMConversation *conversation = [[EaseMob sharedInstance].chatManager conversationForChatter:self.buddy.username conversationType:eConversationTypeChat];
// 獲取會(huì)話中的聊天記錄 (從數(shù)據(jù)庫(kù)中加載消息)
NSArray *msgs = [conversation loadAllMessages];
// 放進(jìn)可變數(shù)組中 改變數(shù)據(jù)
[self.chatMsgs addObjectsFromArray:msgs];// 刷新表格
[self.tableView reloadData];
// 滾到最下面
}
-
(void)binge_observerKeyboardFrameChange {
// 監(jiān)聽(tīng)鍵盤(pán) 對(duì)相應(yīng)的布局做修改
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillChangeFrameNotification
object:nil
queue:[NSOperationQueue mainQueue]// 在哪個(gè)線程執(zhí)行
usingBlock:^(NSNotification * _Nonnull note) {
NSLog(@"%s,line = %d,note = %@",FUNCTION,LINE,note);/* note.userInfo 布局參數(shù) UIKeyboardAnimationCurveUserInfoKey = 7; UIKeyboardAnimationDurationUserInfoKey = "0.25"; UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {414, 271}}"; UIKeyboardCenterBeginUserInfoKey = "NSPoint: {207, 600.5}"; UIKeyboardCenterEndUserInfoKey = "NSPoint: {207, 871.5}"; UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 465}, {414, 271}}"; UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 736}, {414, 271}}"; UIKeyboardIsLocalUserInfoKey = 1; self.view 可以根據(jù) end.oriY 來(lái)進(jìn)行布局更改 */ // 初始高度 CGFloat endY = [note.userInfo [UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y; // 觸發(fā)的鍵盤(pán)高度 CGFloat tempY = endY - self.view.bounds.size.height; self.view.frame = CGRectMake(0, tempY, self.view.bounds.size.width, self.view.bounds.size.height); // 添加動(dòng)畫(huà) CGFloat duration = [note.userInfo [UIKeyboardAnimationDurationUserInfoKey] floatValue]; [UIView animateWithDuration:duration animations:^{ [self.view setNeedsLayout]; }]; }];
}
@end