Commit efaebc22 authored by Alex朱枝文's avatar Alex朱枝文

直播UI调整

parent 64661dfc
......@@ -293,12 +293,14 @@
04564D4B2CF389DD004456E4 /* YHLivePlayerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04564D4A2CF389DD004456E4 /* YHLivePlayerViewController.swift */; };
04564D4D2CF38D16004456E4 /* YHLiveMessageCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04564D4C2CF38D16004456E4 /* YHLiveMessageCell.swift */; };
04564D4F2CF38E20004456E4 /* YHLiveMessageListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04564D4E2CF38E20004456E4 /* YHLiveMessageListView.swift */; };
04564D512CF38EA4004456E4 /* YHMessageInputView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04564D502CF38EA4004456E4 /* YHMessageInputView.swift */; };
04564D532CF38FE2004456E4 /* YHVODPlayerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04564D522CF38FE2004456E4 /* YHVODPlayerViewController.swift */; };
04564D562CF4467B004456E4 /* YHPlayerTopBarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04564D552CF4467B004456E4 /* YHPlayerTopBarView.swift */; };
04564D592CF470B2004456E4 /* YHIncomeRecordCompanyTipsCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04564D582CF470B2004456E4 /* YHIncomeRecordCompanyTipsCell.swift */; };
04564D5D2CF49F0A004456E4 /* live_room_test_bg.png in Resources */ = {isa = PBXBuildFile; fileRef = 04564D5C2CF49F0A004456E4 /* live_room_test_bg.png */; };
04564D5F2CF565C7004456E4 /* YHInputBottomBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04564D5E2CF565C7004456E4 /* YHInputBottomBar.swift */; };
04564D612CF59835004456E4 /* YHMessageInputViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04564D602CF59835004456E4 /* YHMessageInputViewController.swift */; };
04564D632CF60222004456E4 /* YHGradientView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04564D622CF60222004456E4 /* YHGradientView.swift */; };
04564D652CF6065D004456E4 /* YHFadeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04564D642CF6065D004456E4 /* YHFadeView.swift */; };
0457920B2CBCE7B200EBD99B /* YHResignUploadTravelCardViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0457920A2CBCE7B200EBD99B /* YHResignUploadTravelCardViewModel.swift */; };
0457920D2CBCE8A800EBD99B /* YHResignUploadTravelCardListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0457920C2CBCE8A800EBD99B /* YHResignUploadTravelCardListModel.swift */; };
0457920F2CBCE9D000EBD99B /* YHResignUploadTravelCardListTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0457920E2CBCE9D000EBD99B /* YHResignUploadTravelCardListTableViewCell.swift */; };
......@@ -1344,12 +1346,14 @@
04564D4A2CF389DD004456E4 /* YHLivePlayerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHLivePlayerViewController.swift; sourceTree = "<group>"; };
04564D4C2CF38D16004456E4 /* YHLiveMessageCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHLiveMessageCell.swift; sourceTree = "<group>"; };
04564D4E2CF38E20004456E4 /* YHLiveMessageListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHLiveMessageListView.swift; sourceTree = "<group>"; };
04564D502CF38EA4004456E4 /* YHMessageInputView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHMessageInputView.swift; sourceTree = "<group>"; };
04564D522CF38FE2004456E4 /* YHVODPlayerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHVODPlayerViewController.swift; sourceTree = "<group>"; };
04564D552CF4467B004456E4 /* YHPlayerTopBarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHPlayerTopBarView.swift; sourceTree = "<group>"; };
04564D582CF470B2004456E4 /* YHIncomeRecordCompanyTipsCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHIncomeRecordCompanyTipsCell.swift; sourceTree = "<group>"; };
04564D5C2CF49F0A004456E4 /* live_room_test_bg.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = live_room_test_bg.png; sourceTree = "<group>"; };
04564D5E2CF565C7004456E4 /* YHInputBottomBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHInputBottomBar.swift; sourceTree = "<group>"; };
04564D602CF59835004456E4 /* YHMessageInputViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHMessageInputViewController.swift; sourceTree = "<group>"; };
04564D622CF60222004456E4 /* YHGradientView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHGradientView.swift; sourceTree = "<group>"; };
04564D642CF6065D004456E4 /* YHFadeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHFadeView.swift; sourceTree = "<group>"; };
0457920A2CBCE7B200EBD99B /* YHResignUploadTravelCardViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHResignUploadTravelCardViewModel.swift; sourceTree = "<group>"; };
0457920C2CBCE8A800EBD99B /* YHResignUploadTravelCardListModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHResignUploadTravelCardListModel.swift; sourceTree = "<group>"; };
0457920E2CBCE9D000EBD99B /* YHResignUploadTravelCardListTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHResignUploadTravelCardListTableViewCell.swift; sourceTree = "<group>"; };
......@@ -2931,6 +2935,7 @@
isa = PBXGroup;
children = (
04564D482CF385D9004456E4 /* YHBasePlayerViewController.swift */,
04564D602CF59835004456E4 /* YHMessageInputViewController.swift */,
04564D4A2CF389DD004456E4 /* YHLivePlayerViewController.swift */,
04564D522CF38FE2004456E4 /* YHVODPlayerViewController.swift */,
);
......@@ -2944,7 +2949,8 @@
04564D462CF3851D004456E4 /* YHPlayerControlView.swift */,
04564D4C2CF38D16004456E4 /* YHLiveMessageCell.swift */,
04564D4E2CF38E20004456E4 /* YHLiveMessageListView.swift */,
04564D502CF38EA4004456E4 /* YHMessageInputView.swift */,
04564D622CF60222004456E4 /* YHGradientView.swift */,
04564D642CF6065D004456E4 /* YHFadeView.swift */,
04564D552CF4467B004456E4 /* YHPlayerTopBarView.swift */,
04564D5E2CF565C7004456E4 /* YHInputBottomBar.swift */,
);
......@@ -6225,6 +6231,7 @@
04A671602BA0280800C1FB91 /* YHFileRenameInputView.swift in Sources */,
04CA2BF42CB7B33500F36DE7 /* YHResignUploadTravelGuideView.swift in Sources */,
A5ACE93F2B4564F7002C94D2 /* UIViewController+Extension.swift in Sources */,
04564D612CF59835004456E4 /* YHMessageInputViewController.swift in Sources */,
A53B61192BB3C9960010B573 /* YHMyDocListHeaderView.swift in Sources */,
042B20D22CEADBD800655093 /* YHImproveHKView.swift in Sources */,
04F243662C9D5CB200DF2C74 /* YHResignDocumentViewModel.swift in Sources */,
......@@ -6347,6 +6354,7 @@
042B20DA2CEB337100655093 /* YHImproveSchemeTemplateListCell.swift in Sources */,
0445E6AF2BE9CFF6003C52F9 /* YHAppVersionForceUpdateView.swift in Sources */,
040AE9882CEB6DAC00310241 /* YHWorkExperiencePositionModel.swift in Sources */,
04564D652CF6065D004456E4 /* YHFadeView.swift in Sources */,
04213B272C48C95E00797900 /* YHHomeIdentityCell.swift in Sources */,
041892262C91BDF500B9FB94 /* YHResignDocumentHeaderCell.swift in Sources */,
0449EEE92C8EEB1E00A397FD /* YHResinMaterialManageContainerVC.swift in Sources */,
......@@ -6379,6 +6387,7 @@
04F243532C9C434100DF2C74 /* YHHKRecordsCustomerItemCell.swift in Sources */,
A58A8C8F2BA6978100632765 /* YHMineCertificateEntryViewController.swift in Sources */,
045EEF1B2B9F171A0022A143 /* YHAddressPickViewTableViewCell.swift in Sources */,
04564D632CF60222004456E4 /* YHGradientView.swift in Sources */,
04213B2F2C49154900797900 /* YHHomeInfoDetailContainerViewController.swift in Sources */,
047AA3FA2C60739E009C4554 /* YHInvitationWithGiftsSelectItemView.swift in Sources */,
A567E5A42BD7643D00D5D5A0 /* YHSearchInfoCell.swift in Sources */,
......@@ -6448,7 +6457,6 @@
0414BDAB2BC7E9C200225367 /* YHMyPermissionCell.swift in Sources */,
045792152CBD041F00EBD99B /* YHResignUploadTravelHKTableViewCell.swift in Sources */,
04C8F4832B7216D800ADF59B /* YHUserInfoHeaderView.swift in Sources */,
04564D512CF38EA4004456E4 /* YHMessageInputView.swift in Sources */,
045EEEAF2B9F171A0022A143 /* YHWorkExperienceTextItemView.swift in Sources */,
A5ACE9492B4564F7002C94D2 /* YHHUDSuccessView.swift in Sources */,
04B360EF2C620975001EB053 /* YHPrincipleUploadStatusCell.swift in Sources */,
......
......@@ -12,10 +12,35 @@ import UIKit
class YHLivePlayerViewController: YHBasePlayerViewController {
// MARK: - Properties
private var roomInfo: YHLiveRoomInfo
private lazy var messageListView: YHLiveMessageListView = YHLiveMessageListView()
private var listMaxWidth: CGFloat {
return KScreenWidth * 248.0 / 375.0
}
private lazy var bottomInputBar: YHInputBottomBar = {
let view = YHInputBottomBar()
view.textViewTappedEvent = { [weak self] in
guard let self = self else {
return
}
self.present(self.inputVC, animated: false)
}
return view
}()
private lazy var inputVC: YHMessageInputViewController = {
let ctl = YHMessageInputViewController()
ctl.closeCallbcak = {
//
}
ctl.inputCallback = { [weak self] controller, text in
//
}
return ctl
}()
private lazy var messageListView: YHLiveMessageListView = {
let view = YHLiveMessageListView(frame: CGRect.init(x: 0, y: 0, width: listMaxWidth, height: listMaxWidth))
return view
}()
......@@ -40,18 +65,18 @@ class YHLivePlayerViewController: YHBasePlayerViewController {
private func setupUI() {
containerView.addSubview(bottomInputBar)
// containerView.addSubview(messageListView)
containerView.addSubview(messageListView)
bottomInputBar.snp.makeConstraints { make in
make.left.right.bottom.equalToSuperview()
make.top.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-YHInputBottomBar.inputActionHeight)
}
// messageListView.snp.makeConstraints { make in
// make.left.right.equalToSuperview()
// make.bottom.equalTo(bottomInputBar.snp.top)
// make.height.equalTo(view.snp.height).multipliedBy(0.3)
// }
messageListView.snp.makeConstraints { make in
make.left.equalToSuperview().offset(16)
make.bottom.equalTo(bottomInputBar.snp.top).offset(-14)
make.height.width.equalTo(listMaxWidth)
}
}
private func startLive() {
......
//
// YHFadeView.swift
// galaxy
//
// Created by alexzzw on 2024/11/26.
// Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//
import UIKit
class YHFadeView: UIView {
private let maskLayer = CAGradientLayer()
override init(frame: CGRect) {
super.init(frame: frame)
setupMask()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupMask()
}
private func setupMask() {
// 使用黑色渐变作为mask
maskLayer.colors = [
UIColor.white.cgColor,
UIColor.clear.cgColor,
]
maskLayer.startPoint = CGPoint(x: 0.5, y: 0)
maskLayer.endPoint = CGPoint(x: 0.5, y: 1)
// 重要:将渐变层设置为mask
layer.mask = maskLayer
}
override func layoutSubviews() {
super.layoutSubviews()
// 确保mask大小与视图相同
maskLayer.frame = bounds
}
}
//
// YHGradientView.swift
// galaxy
//
// Created by alexzzw on 2024/11/26.
// Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//
import UIKit
class YHGradientView: UIView {
override class var layerClass: AnyClass {
return CAGradientLayer.self
}
init() {
super.init(frame: .zero)
setupGradient()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupGradient()
}
private func setupGradient() {
guard let gradientLayer = layer as? CAGradientLayer else {
return
}
gradientLayer.colors = [
UIColor(white: 1, alpha: 1).cgColor, // 顶部 100% 不透明
UIColor(white: 1, alpha: 0).cgColor // 底部 0% 完全透明
]
gradientLayer.startPoint = CGPoint(x: 0.5, y: 0) // 上边中点
gradientLayer.endPoint = CGPoint(x: 0.5, y: 1) // 下边中点
}
}
......@@ -6,142 +6,65 @@
// Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//
import AttributedString
import UIKit
class YHLiveMessageCell: UITableViewCell {
static let reuseIdentifier = "YHLiveMessageCell"
// MARK: - UI Components
private let containerView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.black.withAlphaComponent(0.4)
private let containerView: YHBaseDynamicCornerRadiusView = {
let view = YHBaseDynamicCornerRadiusView(cornerRadius: 8, corner: .allCorners)
view.backgroundColor = UIColor.black.withAlphaComponent(0.3)
view.layer.cornerRadius = 8
return view
}()
private let levelLabel: YHPaddedLabel = {
let label = YHPaddedLabel(padding: UIEdgeInsets(top: 2, left: 4, bottom: 2, right: 4))
label.font = .systemFont(ofSize: 10)
label.textColor = .white
label.backgroundColor = .systemBlue
label.layer.cornerRadius = 4
label.layer.masksToBounds = true
return label
}()
private let nicknameLabel: UILabel = {
let label = UILabel()
label.font = .systemFont(ofSize: 14, weight: .medium)
label.textColor = .systemYellow
return label
}()
private let contentLabel: UILabel = {
let label = UILabel()
label.font = .systemFont(ofSize: 14)
label.textColor = .white
label.numberOfLines = 0
label.lineBreakMode = .byCharWrapping
return label
}()
// MARK: - Initialization
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupUI()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupUI() {
backgroundColor = .clear
selectionStyle = .none
contentView.addSubview(containerView)
containerView.addSubview(levelLabel)
containerView.addSubview(nicknameLabel)
containerView.addSubview(contentLabel)
setupConstraints()
}
private func setupConstraints() {
containerView.snp.makeConstraints { make in
make.left.equalToSuperview().offset(8)
make.right.lessThanOrEqualToSuperview().offset(-8)
make.top.equalToSuperview().offset(4)
make.bottom.equalToSuperview().offset(-4)
}
levelLabel.snp.makeConstraints { make in
make.left.equalToSuperview().offset(8)
make.centerY.equalTo(nicknameLabel)
}
nicknameLabel.snp.makeConstraints { make in
make.left.equalTo(levelLabel.snp.right).offset(8)
make.top.equalToSuperview().offset(8)
make.left.equalToSuperview()
make.right.lessThanOrEqualToSuperview()
make.top.equalToSuperview().offset(2)
make.bottom.equalToSuperview().offset(-2)
}
contentLabel.snp.makeConstraints { make in
make.left.equalToSuperview().offset(8)
make.right.equalToSuperview().offset(-8)
make.top.equalTo(nicknameLabel.snp.bottom).offset(4)
make.bottom.equalToSuperview().offset(-8)
}
}
// MARK: - Configuration
func configure(with message: YHLiveMessage) {
levelLabel.text = "Lv.\(message.sender.level)"
nicknameLabel.text = message.sender.nickname
switch message.type {
case .normal:
configureNormalMessage(message)
case .system:
configureSystemMessage(message)
case .gift:
configureGiftMessage(message)
case .enter:
configureEnterMessage(message)
case .like:
configureLikeMessage(message)
}
}
private func configureNormalMessage(_ message: YHLiveMessage) {
contentLabel.text = message.content
contentLabel.textColor = .white
if message.sender.isAdmin {
nicknameLabel.textColor = .systemRed
} else {
nicknameLabel.textColor = .systemYellow
make.top.equalToSuperview().offset(4)
make.bottom.equalToSuperview().offset(-4)
}
}
private func configureSystemMessage(_ message: YHLiveMessage) {
levelLabel.isHidden = true
nicknameLabel.isHidden = true
contentLabel.text = message.content
contentLabel.textColor = .systemGray
contentLabel.textAlignment = .center
}
private func configureGiftMessage(_ message: YHLiveMessage) {
contentLabel.text = message.content
contentLabel.textColor = .systemPink
}
private func configureEnterMessage(_ message: YHLiveMessage) {
contentLabel.text = "进入了直播间"
contentLabel.textColor = .systemGreen
}
private func configureLikeMessage(_ message: YHLiveMessage) {
contentLabel.text = "点了赞"
contentLabel.textColor = .systemPink
func configureNormalMessage(_ nickname: String, _ content: String) {
let nickAtt = ASAttributedString(string: nickname + ":", .foreground(UIColor.white.withAlphaComponent(0.65)), .font(UIFont.PFSC_R(ofSize: 13)))
let contentAtt = ASAttributedString(string: content, .foreground(UIColor.white), .font(UIFont.PFSC_R(ofSize: 13)))
contentLabel.attributed.text = nickAtt + contentAtt
}
}
......@@ -9,118 +9,106 @@
import UIKit
class YHLiveMessageListView: UIView {
// MARK: - Properties
private var messages: [YHLiveMessage] = []
private let maxMessageCount = 200
// MARK: - UI Components
private lazy var tableView: UITableView = {
let tableView = UITableView()
tableView.backgroundColor = .clear
tableView.separatorStyle = .none
tableView.showsVerticalScrollIndicator = false
tableView.allowsSelection = false
tableView.delegate = self
tableView.dataSource = self
tableView.register(YHLiveMessageCell.self, forCellReuseIdentifier: YHLiveMessageCell.reuseIdentifier)
return tableView
}()
private lazy var messageInputView: YHMessageInputView = {
let view = YHMessageInputView(frame: CGRect.zero)
private lazy var topFadeView: YHFadeView = {
let view = YHFadeView()
view.backgroundColor = .clear.withAlphaComponent(0.1)
return view
}()
private lazy var tableView: UITableView = {
let view = UITableView()
view.backgroundColor = .clear
view.separatorStyle = .none
view.showsVerticalScrollIndicator = false
view.allowsSelection = false
view.delegate = self
view.dataSource = self
view.transform = CGAffineTransform(rotationAngle: .pi)
view.register(YHLiveMessageCell.self, forCellReuseIdentifier: YHLiveMessageCell.reuseIdentifier)
return view
}()
// MARK: - Initialization
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
setupData()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupData() {
let message6 = YHLiveMessage.init(type: .normal, sender: YHLiveUser.init(userId: "1233", nickname: "MAKESIBIB", level: 0, isAdmin: false), content: "优才今年政策", timestamp: Date())
let message5 = YHLiveMessage.init(type: .normal, sender: YHLiveUser.init(userId: "1233", nickname: "MAKESIBIB", level: 0, isAdmin: false), content: "😊", timestamp: Date())
let message4 = YHLiveMessage.init(type: .normal, sender: YHLiveUser.init(userId: "1233", nickname: "姑息可", level: 0, isAdmin: false), content: "优才申请时间有什么限制吗", timestamp: Date())
let message3 = YHLiveMessage.init(type: .normal, sender: YHLiveUser.init(userId: "1233", nickname: "侯米儿", level: 0, isAdmin: false), content: "我来了", timestamp: Date())
let message2 = YHLiveMessage.init(type: .normal, sender: YHLiveUser.init(userId: "1233", nickname: "奇思幻想牛先生", level: 0, isAdmin: false), content: "可以帮忙介绍一下香港优秀人才计划吗?", timestamp: Date())
let message1 = YHLiveMessage.init(type: .normal, sender: YHLiveUser.init(userId: "1233", nickname: "侯米儿", level: 0, isAdmin: false), content: "我工作5年,香港高才怎么申请更快获批啊", timestamp: Date())
let message0 = YHLiveMessage.init(type: .normal, sender: YHLiveUser.init(userId: "1233", nickname: "谷溪", level: 0, isAdmin: false), content: "想了解香港教育有什么优势", timestamp: Date())
messages = [message0, message1, message2, message3, message4, message5, message6]
// messages = [message6]
tableView.reloadData()
}
private func setupUI() {
backgroundColor = .clear
addSubview(tableView)
addSubview(messageInputView)
setupConstraints()
}
private func setupConstraints() {
tableView.snp.makeConstraints { make in
addSubview(topFadeView)
bringSubviewToFront(topFadeView)
topFadeView.snp.makeConstraints { make in
make.top.left.right.equalToSuperview()
make.bottom.equalTo(messageInputView.snp.top)
make.height.equalTo(26)
}
messageInputView.snp.makeConstraints { make in
make.left.right.bottom.equalToSuperview()
make.height.equalTo(44)
tableView.snp.makeConstraints { make in
make.top.equalToSuperview()
make.left.bottom.right.equalToSuperview()
}
}
// MARK: - Public Methods
func addMessage(_ message: YHLiveMessage) {
messages.append(message)
if messages.count > maxMessageCount {
messages.removeFirst(messages.count - maxMessageCount)
}
let indexPath = IndexPath(row: messages.count - 1, section: 0)
tableView.insertRows(at: [indexPath], with: .fade)
scrollToBottom(animated: true)
messages.insert(message, at: 0)
let indexPath = IndexPath(row: 0, section: 0)
tableView.insertRows(at: [indexPath], with: .bottom)
}
func clearMessages() {
messages.removeAll()
tableView.reloadData()
}
private func scrollToBottom(animated: Bool) {
guard !messages.isEmpty else { return }
let lastIndex = IndexPath(row: messages.count - 1, section: 0)
tableView.scrollToRow(at: lastIndex, at: .bottom, animated: animated)
}
}
// MARK: - UITableViewDelegate & UITableViewDataSource
extension YHLiveMessageListView: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messages.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard messages.count > indexPath.row else {
return UITableViewCell()
}
guard let cell = tableView.dequeueReusableCell(
withIdentifier: YHLiveMessageCell.reuseIdentifier,
for: indexPath) as? YHLiveMessageCell else {
withIdentifier: YHLiveMessageCell.reuseIdentifier) as? YHLiveMessageCell else {
return UITableViewCell()
}
let message = messages[indexPath.row]
cell.configure(with: message)
// cell 也需要旋转
cell.contentView.transform = CGAffineTransform(rotationAngle: .pi)
cell.configureNormalMessage(message.sender.nickname, message.content)
return cell
}
}
// MARK: - MessageInputViewDelegate
extension YHLiveMessageListView: YHMessageInputViewDelegate {
func messageInputView(_ inputView: YHMessageInputView, didSendMessage text: String) {
// 创建当前用户
let currentUser = YHLiveUser(userId: "current_user",
nickname: "我",
level: 1,
isAdmin: false)
// 创建消息
let message = YHLiveMessage.createNormalMessage(sender: currentUser,
content: text)
// 添加消息到列表
addMessage(message)
}
}
//
// YHMessageInputView.swift
// galaxy
//
// Created by alexzzw on 2024/11/25.
// Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//
import UIKit
protocol YHMessageInputViewDelegate: AnyObject {
func messageInputView(_ inputView: YHMessageInputView, didSendMessage text: String)
}
enum YHMessageInputViewState {
case normalWithGift(_ count: Int)
case inputState
}
class YHMessageInputView: UIView {
// MARK: - Properties
weak var delegate: YHMessageInputViewDelegate?
// MARK: - UI Components
private lazy var textField: UITextField = {
let textField = UITextField()
textField.placeholder = "说点什么..."
textField.textColor = .white
textField.backgroundColor = UIColor.white.withAlphaComponent(0.2)
textField.layer.cornerRadius = 17
textField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 34))
textField.leftViewMode = .always
textField.returnKeyType = .send
textField.delegate = self
return textField
}()
private lazy var giftButton: UIButton = {
let button = UIButton(type: .custom)
button.setBackgroundImage(UIImage(named: "live_room_msg_gift"), for: .normal)
button.setTitle("2", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = .PFSC_M(ofSize: 10)
button.addTarget(self, action: #selector(giftButtonTapped), for: .touchUpInside)
return button
}()
private lazy var sendButton: UIButton = {
let button = UIButton(type: .custom)
button.setTitle("发送", for: .normal)
button.titleLabel?.font = .PFSC_M(ofSize: 12)
button.setTitleColor(.white, for: .normal)
button.backgroundColor = .brandMainColor
button.layer.cornerRadius = 3
button.clipsToBounds = true
button.addTarget(self, action: #selector(sendButtonTapped), for: .touchUpInside)
return button
}()
var state: YHMessageInputViewState = .normalWithGift(2) {
didSet {
updateState(state)
}
}
// MARK: - Initialization
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
updateState(state)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupUI() {
addSubview(textField)
addSubview(sendButton)
addSubview(giftButton)
}
private func updateState(_ state: YHMessageInputViewState) {
// switch state {
// case let .normalWithGift(count):
// <#code#>
// case .inputState:
// <#code#>
// }
textField.snp.makeConstraints { make in
make.left.equalToSuperview().offset(8)
make.centerY.equalToSuperview()
make.right.equalTo(sendButton.snp.left).offset(-8)
make.height.equalTo(34)
}
sendButton.snp.makeConstraints { make in
make.right.equalToSuperview().offset(-8)
make.centerY.equalToSuperview()
make.width.equalTo(50)
}
}
@objc private func sendButtonTapped() {
sendMessage()
}
@objc private func giftButtonTapped() {
//
}
private func sendMessage() {
guard let text = textField.text?.trimmingCharacters(in: .whitespacesAndNewlines),
!text.isEmpty else {
return
}
delegate?.messageInputView(self, didSendMessage: text)
textField.text = ""
}
}
// MARK: - UITextFieldDelegate
extension YHMessageInputView: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
sendMessage()
textField.resignFirstResponder()
return true
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment