iOS OC語言原生開發的IM模塊,用於項目中需要原生開發IM的情況,具備發送文字、表情、語音、圖片、視頻等完整功能,包含圖片預覽視頻播放等功能,此項目將會長期更新如有問題可以提出,我的郵箱:fshmjl@aliyun.com,我將盡快解決; 項目地址:https://github.com/fshmjl/RChat
效果圖如下
項目介紹
項目中對輸入框等模塊功能進行了封裝,方便復用或重寫
輸入框模塊 InputBox
輸入框功能頁面 KInputBoxView.m
輸入框表情頁面 KInputBoxViewCtrl.m
輸入框更多頁面 KInputBoxMoreView.m
輸入框部分代理 KInputBoxViewDelegate
KInputBoxViewDelegate提供的代理
@protocol KInputBoxViewDelegate <NSObject> @optional #pragma mark - 表情頁面(KInputBoxEmojiView)代理 /** 點擊選擇表情 @param emojiView 表情所在頁面 @param emojiDic 表情數據 @param emojiType 表情類型 */ - (void)emojiView:(KInputBoxEmojiView *)emojiView didSelectEmoji:(KEmojiModel *)emojiDic emojiType:(KEmojiType)emojiType; /** 刪除光標前面的表情 */ - (void)emojiViewDeleteEmoji; /** 點擊發送按鈕,發送表情 @param emojiView 表情菜單 @param emojiStr 發送按鈕 */ - (void)emojiView:(KInputBoxEmojiView *)emojiView sendEmoji:(NSString *)emojiStr; #pragma mark - 表情菜單代理部分 /** 點擊添加表情按鈕 @param menuView 表情菜單 @param addBut 點擊按鈕 */ - (void)emojiMenuView:(KInputBoxEmojiMenuView *)menuView clickAddAction:(UIButton *)addBut; /** 選擇表情組 @param menuView 表情菜單頁面 @param emojiGroup 表情組 */ - (void)emojiMenuView:(KInputBoxEmojiMenuView *)menuView didSelectEmojiGroup:(KEmojiGroup *)emojiGroup; /** 點擊發送按鈕,發送表情 @param menuView 表情菜單 @param sendBut 發送按鈕 */ - (void)emojiMenuView:(KInputBoxEmojiMenuView *)menuView sendEmoji:(UIButton *)sendBut; #pragma mark - 輸入框代理部分 /** 通過輸入的文字的變化,改變輸入框的高度 @param inputBox 輸入框 @param height 改變的高度 */ - (void)inputBox:(KInputBoxView *)inputBox changeInputBoxHeight:(CGFloat)height; /** 發送消息 @param inputBox 輸入框 @param textMessage 輸入的文字內容 */ - (void)inputBox:(KInputBoxView *)inputBox sendTextMessage:(NSString *)textMessage; /** 狀態改變 @param inputBox 輸入框 @param fromStatus 上一個狀態 @param toStatus 當前狀態 */ - (void)inputBox:(KInputBoxView *)inputBox changeStatusForm:(KInputBoxStatus)fromStatus to:(KInputBoxStatus)toStatus; /** 點擊輸入框更多按鈕事件 @param inputBox 輸入框 @param inputStatus 當前狀態 */ - (void)inputBox:(KInputBoxView *)inputBox clickMoreInput:(KInputBoxStatus)inputStatus;
會話頁面
會話頁面其實不用多說的,就是一個普通的UITableView,如果需要重寫會話視圖,只需要對其Cell(KMessagesListTableViewCell)進行改動即可
重點介紹聊天頁面
眾所周知聊天頁面也是一個UITableView,其實聊天頁面真正繁瑣的是不同消息類型的不同Cell問題,還有就是在消息頁面中布局的問題,由於消息頁面出現不同的Cell較多,而且刷新頻繁,所有要考慮很多UITableView優化的問題,如布局問題,AutoLayout雖然在布局方面有優勢,但是會使性能下降,所以在RChat中消息頁面及Cell中幾乎都是使用的Frame布局,主要是為了提升性能。另一個問題就是計算行高的問題,對於UITableView優化已經是一個老生常談的問題,我們Cell是需要先設置行高的,所以一般都需要對行高進行緩存,避免系統多次計算。 在寫這個項目的時候我也看過好幾篇IM界面的項目或者說demo,都不是很完整,幾乎是不能直接使用的,所以寫了這個項目,項目很多東西都是可以重寫,繼承和擴展。聊天消息支持的類型有文字(包含表情)、圖片、視頻、語音和郵件,其他類型需要根據需要自己定義,但是定義的時候建議繼承KChatTableViewCell,方便統一處理。
消息基類 KChatTableViewCell
文本消息(包含表情) KChatTextTableViewCell
圖片消息 KChatImageTableViewCell
視頻消息 KChatVideoTableViewCell
語音消息 KChatVoiceTableViewCell
郵件消息 KChatMailTableViewCell
消息代理 KChatTableViewCellDelegate
KChatTableViewCellDelegate中提供的方法
@protocol KChatTableViewCellDelegate <NSObject> /** 點擊cell中的頭像 @param tableViewCell 當前cell @param messageModel 當前cell的數據 */ - (void)chatTableViewCell:(KChatTableViewCell *)tableViewCell clickAvatarImageViewMessageModel:(KMessageModel *)messageModel; /** 點擊消息背景 @param tableViewCell 當前cell @param messageModel 當前cell的數據 */ - (void)chatTableViewCell:(KChatTableViewCell *)tableViewCell clickBackgroudImageViewMessageModel:(KMessageModel *)messageModel; /** 當發送失敗時點擊,發送狀態展示視圖 @param tableViewCell 當前cell @param conversationModel 會話信息 @param messageModel 消息 */ - (void)chatTableViewCell:(KChatTableViewCell *)tableViewCell clickResendMessageWithConversationModel:(KConversationModel *)conversationModel messageModel:(KMessageModel *)messageModel; /** 點擊回復郵件 @param tableViewCell 當前cell @param messageModel 當前cell的數據 */ - (void)chatTableViewCell:(KChatMailTableViewCell *)tableViewCell replyMailMessageModel:(KMessageModel *)messageModel; /** 點擊回復全部 @param tableViewCell 當前cell @param messageModel 當前cell的數據 */ - (void)chatTableViewCell:(KChatMailTableViewCell *)tableViewCell replyAllMaillMessageModel:(KMessageModel *)messageModel; /** 點擊轉發郵件 @param tableViewCell 當前cell @param messageModel 當前cell的數據 */ - (void)chatTableViewCell:(KChatMailTableViewCell *)tableViewCell transmitMailMessageModel:(KMessageModel *)messageModel; /** 點擊語音消息 @param tableViewCell 當前cell @param messageModel 當前數據 */ - (void)chatTableViewCell:(KChatVoiceTableViewCell *)tableViewCell clickVoiceMessageMessageModel:(KMessageModel *)messageModel;
聊天消息控制器 KChatViewController 控制器使用Category的方式分類開發
頁面定義和文本消息 KChatViewController
語音部分 KChatViewController+Voice
圖片和視頻部分 KChatViewController+Image
消息頁面提供幾個經常使用的方法
// 重新刷新數據 - (void)reloadData; // 移動到底部 - (void)scrollTableViewBottom ; // 添加一條消息 - (void)addMessage:(KMessageModel *)model; // 最后一條消息 - (KMessageModel *)lastMessage;
發送一條消息是,需要構造一個KMessageModel,調用addMessage:就可以,但是需要做緩存和上傳到服務端的代碼需要根據自己的需求寫。
在項目中有什么疑問的,或者存在bug的,都可以給我提Issues,描述清楚問題和重現步驟,我將第一時間更新,如果有需要幫忙的同學可以發郵件到fshmjl@aliyun.com,最后麻煩大家點個Star喲。