精致的 UIKit 聊天界面库。
ChatUIKit
是一个用于构建聊天应用的 Swift 轻量化框架。它被设计成可扩展和性能强大。看看它的实际应用效果!
特性
- 气泡颜色:我、朋友和其他。
- 气泡的头部和尾部类型取决于它是否是同一发送者气泡组的成员,或者在两个或单个气泡的组中。
- 气泡位置会自动重置,始终使最后一个气泡位于文本输入框的上方。
- 文本输入框的高度可以根据行数自适应,最多支持四行,之后变为可滚动。
- 文本气泡
- 图片气泡
- 可扩展输入栏
安装方式
CocoaPods
-
请确保您的
Podfile
中已添加use_frameworks!
。 -
在您的
Podfile
中包含以下内容:
pod 'ChatUIKit'
- 运行
pod install
如何使用
从 ChatViewController 继承
在继承的情况下需要调用 super.init(),然后初始化 ChatDataSource 及其成员。
/*
The struct User must implement getter for objejectId and name and
function == because SenderProtocol must conform Equatable.
*/
struct User: SenderProtocol {
var objectId: String?
var name: String?
public static func == (lhs: Person, rhs: Person) -> Bool {
return lhs.objectId == rhs.objectId
}
}
/*
The class MyMessage must implement getter for message, sender, timeStr, and dayStr.
ChatUIKit provides a Date extension that export Date to time and day, see
below the implementation.
*/
struct MyMessage: Message {
var date: Date?
var message: String?
var sender: User?
var timeStr: String? { get { return self.date?.chatUIKitTimeStr } }
var dayStr: String? { get { return self.date?.chatUIKitDayStr } }
public static func == (lhs: MyMessage, rhs: MyMessage) -> Bool {
return lhs.objectId == rhs.objectId
}
}
class MyChatViewController: ChatViewController {
MyChatViewController(user: User) {
super.init()
let user = User.current()
let chatDataSource = ChatDataSource<MyMessage, User>(owner: user, otherName:"Admin")
self.dataSource = chatDataSource
self.delegate = chatDataSource
self.appointment = appointment
self.chatPartner = chatPartner
self.sendMessageBlock = { (_ message:String, _ succed:@escaping ()->Void) in
// Your code probably should send the message to your server and only when gets
// the confirmation it will update the chat view controller or notifying about what
// was the problem.
guard let chatDataSource = self.dataSource as?
ChatDataSource<PersonMessage, Person> else { return }
var message = MyMessage()
message.message = message
let user = User.current()
message.sender = user
if chatDataSource.chatList.contains( where: { $0 == message } ) { return }
chatDataSource.chatList.append(message)
chatViewController.tableView.reloadData()
chatViewController.resetTablePosition()
succed()
}
}
}
Storyboard 与实现容器视图
Be sure that the class ChatViewController in your storyboard is pointing to module ChatUIKit.
class MyContainerChatViewController: UIViewController {
var chatViewController : ChatViewController?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let identifier = segue.identifier,
identifier == "MyContainerToChatViewControllerSegue",
let chatViewController = segue.destination as? ChatViewController {
self.chatViewController = chatViewController
let user = User.current()
let chatDataSource = ChatDataSource<MyMessage, User>(owner: user, otherName:"Admin")
chatViewController.dataSource = chatDataSource
chatViewController.delegate = chatDataSource
chatViewController.sendMessageBlock = {
(_ message:String, _ succed:@escaping ()->Void) in
// Same implementation as before.
}
}
}
用新消息更新 Chat 视图控制器
class MyContainerChatViewController: UIViewController {
var chatViewController : ChatViewController?
func messageArrive( message: MyMessage ){
guard let chatDataSource = self.dataSource as? ChatDataSource<MyMessage, User> else {
return
}
if chatDataSource.chatList.contains( where: { $0 == message } ) { return }
chatDataSource.chatList.append(message)
chatViewController.tableView.reloadData()
chatViewController.resetTablePosition()
}
}