CDChatList

Github.com

gif1.GIF

gif2.GIF

高性能的聊天页面解决方案
对聊天列表的高度封装,可灵活配置页面样式

聊天界面其实大同小异,所以这里封装了一个聊天的组件,使用CoreText和手动代码布局,尽量实现简单,通用,高效,易于维护。

项目结构

CDChatListView: UITableView 视图,聊天页面主体

CDBaseMsgCell: 实现消息气泡基本视图

CDTextTableViewCell、CDImageTableViewCell、CDAudioTableViewCell: 继承自CDBaseMsgCell,实现响应功能。

CDSystemTableViewCell: 特殊消息气泡,实现系统通知

CellCaculator: tableview布局计算,并提前渲染cell

ChatConfiguration: chatlist配置类组,UI定制,及资源等

子组件

CDLabel: 富文本标签

CDChatInputBox: 输入框封装组件

安装

支持至iOS 11

pod 'CDChatList'

使用

配置 CDChatList

ChatHelpr负责ChatHelpr的UI配置,及组件的资源文件设置

UI配置及资源文件都有默认,所以无需自定义的话,就可以跳过组件的配置

添加 CDChatList 视图

CDChatListView *list = [[CDChatListView alloc] initWithFrame:self.view.bounds];
list.msgDelegate = self;
self.listView = list;
[self.view addSubview:self.listView];

CDChatList会将视图控制器automaticallyAdjustsScrollViewInsets及contentInsetAdjustmentBehavior设为NO及Never,并适应导航栏高度

消息模型 MessageModalProtocal

可以使用自己的消息模型,消息模型需遵守MessageModalProtocal,实现相关属性

组件事件 ChatListProtocol

从组件发出的消息

消息列表请求加载更多消息

-(void)chatlistLoadMoreMsg: (CDChatMessage)topMessage
callback: (void(^)(CDChatMessageArray))finnished;

消息中的点击事件

-(void)chatlistClickMsgEvent: (ChatListInfo *)listInfo;

向组件发消息

添加新的数据到底部

-(void)addMessagesToBottom: (CDChatMessageArray)newBottomMsgArr;

更新数据源中的某条消息模型(主要是为了更新UI上的消息状态)

-(void)updateMessage:(CDChatMessage)message;

使用场景

收/发消息

// 发
{
    MessageModal *modal;
}
-(void)send{
    modal = [[MessageModal alloc] init];
    modal.msgState = CDMessageStateSending;
    modal.createTime = ...;
    modal.msg = ...;
    modal.msgType = ...;
    [chatList addMessagesToBottom: modal];
}

-(void)sendCallBack:(BOOL)isSuccess{
    modal.msgState = isSuccess;  // 此处应处理成枚举
    [chatList updateMessage: modal];
}



// 收
-(void)receivedNewMessage:(MessageModal *)modal{
    [chatList addMessagesToBottom: modal];
}

下拉加载更多消息

消息列表被下拉时,触发此回调

-(void)chatlistLoadMoreMsg: (CDChatMessage)topMessage
callback: (void(^)(CDChatMessageArray))finnished
{
    // 根据topMessage 获取更多消息
    NSArray *msgArr = [self getMoreMessageFrom: topMessage amount: 10];
    callback(msgArr);
}

消息点击事件

目前消息体重处理了 文本点击 及 图片点击 事件

-(void)chatlistClickMsgEvent: (ChatListInfo *)listInfo{
    if (listInfo.eventType == ChatClickEventTypeTEXT){
        // 点击的文本
        listInfo.clickedText
        // 点击的文字位置  防止有相同的可点击文字
        listInfo.range
        // 被点击文本的隐藏信息   e.g.  <a title="转人工" href="doTransfer">
        listInfo.clickedTextContent
    } else if (listInfo.eventType == ChatClickEventTypeIMAGE){
        // 图片
        listInfo.image
        // 图片在tableview中的位置
        listInfo.msgImageRectInTableView
    }
}

TODO

  • 自定义消息内容匹配
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,172评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,195评论 4 61
  • 带点鸭脖花生米麻辣烫小龙虾小丸子炸鸡排手抓饼两瓶可乐盐酥鸡东坡肘子酱牛肉糖醋排骨红烧肉水煮肉片水煮鱼番茄牛腩炸鸡腿...
    李欣悦1996L阅读 793评论 0 0
  • 文/安安安小软 图/冷饮 . . . <本期完> 饲养员Saying ▼ 夏周一在商场摸爬滚打多年 终于因为 妨碍...
    全民备胎夏周一阅读 1,274评论 0 0
  • 特蕾莎是个生活在遥远西部北方的乡村姑娘。她时常感到孤独,她的孤独隐藏在内心深处,无人碰及。她喜欢对着天空发呆,然后...
    S简阅读 4,097评论 0 0