MXRMessenger 是一个可定制的聊天库,旨在提供流畅滚动和响应式体验。我之所以要编写这个库,是因为:
话虽如此,如果您从未使用过 Texture,您可能最好选择上述更成熟的库之一。这个库也几乎未经测试,到目前为止仅在1个生产应用程序中使用,所以请自行承担风险。
MXRMessengerViewController
类似于 SlackTextViewController 的简化版,可以单独通过 pod 'MXRMessenger/ViewController'
包括。它具有:
其他 subspec pod 'MXRMessenger/MessageCell'
提供带有友好工厂的 Facebook 风格气泡。主要特点:
并且因为我们依赖于 Texture,您还可以免费获得 60 fps 的流畅度。
MXRMessenger 可以通过 CocoaPods 获取。
对于完整的聊天库,请在 Podfile 中添加 pod 'MXRMessenger'
。
如果只需要 ViewController,请添加 pod 'MXRMessenger/ViewController'
。
如果只需要 Facebook 风格的气泡,请添加 pod 'MXRMessenger/MessageCell'
。
需要在必要时添加
#import <MXRMessenger/MXRMessenger.h>
继承 MXRMessengerViewController
子类。然后,使用提供的 MXRMessageCellFactory
是实例化信息单元格的最简单方法。因此创建一个强属性
@property (nonatomic, strong) MXRMessageCellFactory* cellFactory;
大多数自定义操作在初始化时发生。您可以复制粘贴以下代码,仅删除或自定义您想要的任何内容。然后实现编译器关于代理或数据源方法的抱怨。
- (instancetype)init {
MXRMessengerInputToolbar* toolbar = [[MXRMessengerInputToolbar alloc] initWithFont:[UIFont systemFontOfSize:16.0f] placeholder:@"Type a message" tintColor:[UIColor mxr_fbMessengerBlue]];
self = [super initWithToolbar:toolbar];
if (self) {
// add extra buttons to toolbar
MXRMessengerIconButtonNode* addPhotosBarButtonButtonNode = [MXRMessengerIconButtonNode buttonWithIcon:[[MXRMessengerPlusIconNode alloc] init] matchingToolbar:self.toolbar];
[addPhotosBarButtonButtonNode addTarget:self action:@selector(tapAddPhotos:) forControlEvents:ASControlNodeEventTouchUpInside];
self.toolbar.leftButtonsNode = addPhotosBarButtonButtonNode;
[self.toolbar.defaultSendButton addTarget:self action:@selector(tapSend:) forControlEvents:ASControlNodeEventTouchUpInside];
// delegate must be self for interactive keyboard, datasource can be whatever
self.node.tableNode.delegate = self;
self.node.tableNode.dataSource = self;
[self customizeCellFactory];
}
return self
}
- (void)customizeCellFactory {
MXRMessageCellLayoutConfiguration* layoutConfigForMe = [MXRMessageCellLayoutConfiguration rightToLeft];
MXRMessageCellLayoutConfiguration* layoutConfigForOthers = [MXRMessageCellLayoutConfiguration leftToRight];
MXRMessageAvatarConfiguration* avatarConfigForMe = nil;
MXRMessageAvatarConfiguration* avatarConfigForOthers = [[MXRMessageAvatarConfiguration alloc] init];
MXRMessageTextConfiguration* textConfigForMe = [[MXRMessageTextConfiguration alloc] initWithFont:nil textColor:[UIColor whiteColor] backgroundColor:[UIColor mxr_fbMessengerBlue]];
MXRMessageTextConfiguration* textConfigForOthers = [[MXRMessageTextConfiguration alloc] initWithFont:nil textColor:[UIColor blackColor] backgroundColor:[UIColor mxr_bubbleLightGrayColor]];
CGFloat maxCornerRadius = textConfigForMe.maxCornerRadius;
MXRMessageImageConfiguration* imageConfig = [[MXRMessageImageConfiguration alloc] init];
imageConfig.maxCornerRadius = maxCornerRadius;
MXRMessageMediaCollectionConfiguration* mediaCollectionConfig = [[MXRMessageMediaCollectionConfiguration alloc] init];
mediaCollectionConfig.maxCornerRadius = maxCornerRadius;
textConfigForMe.menuItemTypes |= MXRMessageMenuItemTypeDelete;
textConfigForOthers.menuItemTypes |= MXRMessageMenuItemTypeDelete;
imageConfig.menuItemTypes |= MXRMessageMenuItemTypeDelete;
imageConfig.showsUIMenuControllerOnLongTap = YES;
CGFloat s = [UIScreen mainScreen].scale;
imageConfig.borderWidth = s > 0 ? (1.0f/s) : 0.5f;
MXRMessageCellConfiguration* cellConfigForMe = [[MXRMessageCellConfiguration alloc] initWithLayoutConfig:layoutConfigForMe avatarConfig:avatarConfigForMe textConfig:textConfigForMe imageConfig:imageConfig mediaCollectionConfig:mediaCollectionConfig];
MXRMessageCellConfiguration* cellConfigForOthers = [[MXRMessageCellConfiguration alloc] initWithLayoutConfig:layoutConfigForOthers avatarConfig:avatarConfigForOthers textConfig:textConfigForOthers imageConfig:imageConfig mediaCollectionConfig:mediaCollectionConfig];
self.cellFactory = [[MXRMessageCellFactory alloc] initWithCellConfigForMe:cellConfigForMe cellConfigForOthers:cellConfigForOthers];
self.cellFactory.dataSource = self;
self.cellFactory.contentNodeDelegate = self;
self.cellFactory.mediaCollectionDelegate = self;
}
要创建信息单元格,您可以实现如下 ASTableDataSource
方法
- (ASCellNodeBlock)tableNode:(ASTableNode *)tableNode nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath {
Message* message = self.messages[indexPath.row];
if (message.media.count > 1) {
return [self.cellFactory cellNodeBlockWithMedia:message.media tableNode:tableNode row:indexPath.row];
} else if (message.media.count == 1) {
MessageMedium* medium = message.media.firstObject;
return [self.cellFactory cellNodeBlockWithImageURL:medium.photoURL showsPlayButton:(medium.videoURL != nil) tableNode:tableNode row:indexPath.row];
} else {
return [self.cellFactory cellNodeBlockWithText:message.text tableNode:tableNode row:indexPath.row];
}
}
要发送新信息或更新带有新/旧信息的多米诺表格,只需在 cellFactory
上有一个方法。
- (void)updateTableNode:(ASTableNode*)tableNode animated:(BOOL)animated withInsertions:(NSArray<NSIndexPath*>*)insertions deletions:(NSArray<NSIndexPath*>*)deletions reloads:(NSArray<NSIndexPath*>*)reloads completion:(void(^)(BOOL))completion;
请记住以下几个关键假设
cellFactory
上的方法编程地显示/隐藏它们(例如,当用户点击单元格时)。MXRMessageCellFactoryDataSource
方法来进行的。这意味着您应该保持这些方法快速 - 理想情况下,它们只是字典查找。有关更多详细信息,最好检查示例项目。
要运行示例项目,请克隆仓储库,并在包含 Podfile
的 Example 目录中运行 pod install
。然后打开 *.xcworkspace
文件并进行构建。
Texture 2.X 和 iOS 8+。
目前没有测试。请确保PR保持非常小。
Scott Kensell,[email prot
MXRMessenger 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。