125 lines
2.9 KiB
Swift
125 lines
2.9 KiB
Swift
//
|
|
// ChatSendVoiceView.swift
|
|
// yinmeng-ios
|
|
//
|
|
// Created by MaiMang on 2024/3/1.
|
|
//
|
|
|
|
import UIKit
|
|
import NIMSDK
|
|
class ChatSendVoiceView: UIView {
|
|
|
|
private var backView: UIView = {
|
|
let view = UIView()
|
|
view.backgroundColor = .black
|
|
view.layer.masksToBounds = true
|
|
view.layer.cornerRadius = 10
|
|
return view
|
|
}()
|
|
|
|
private var logoImageView: UIImageView = {
|
|
let imageView = UIImageView()
|
|
imageView.isUserInteractionEnabled = true
|
|
let firstImage = UIImage(named: "chat_voice_record_first")!
|
|
let secondImage = UIImage(named: "chat_voice_record_second")!
|
|
let thirdImage = UIImage(named: "chat_voice_record_third")!
|
|
imageView.animationImages = [firstImage, secondImage, thirdImage]
|
|
imageView.animationDuration = 1
|
|
imageView.animationRepeatCount = .max
|
|
return imageView
|
|
}()
|
|
|
|
private var titleLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.font = UIFont.systemFont(ofSize: 15)
|
|
label.textColor = .white
|
|
label.textAlignment = .center
|
|
return label
|
|
}()
|
|
|
|
private var timeLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.font = UIFont.systemFont(ofSize: 17, weight: .medium)
|
|
label.textColor = .white
|
|
label.textAlignment = .center
|
|
return label
|
|
}()
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
loadSubViews()
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
|
|
func configAudioRecord(imageName: String, title: String, isAnimation: Bool) {
|
|
logoImageView.image = UIImage(named: imageName)
|
|
if isAnimation {
|
|
logoImageView.startAnimating()
|
|
} else {
|
|
logoImageView.stopAnimating()
|
|
}
|
|
titleLabel.text = title
|
|
}
|
|
|
|
func beginAudioRecord() {
|
|
timeLabel.text = "00:00"
|
|
NIMSDK.shared().mediaManager.record(forDuration: 60)
|
|
}
|
|
|
|
func cancelAudioRecord() {
|
|
timeLabel.text = "00:00"
|
|
NIMSDK.shared().mediaManager.cancelRecord()
|
|
}
|
|
|
|
func finishAudioRecord() {
|
|
timeLabel.text = "00:00"
|
|
NIMSDK.shared().mediaManager.stopRecord()
|
|
}
|
|
|
|
func updateAudioRecordProgress(recordTime: TimeInterval) {
|
|
let minutes = Int(recordTime) / 60
|
|
let seconds = Int(recordTime) % 60
|
|
timeLabel.text = String(format: "%02d:%02d", minutes, seconds)
|
|
}
|
|
|
|
// Private Method
|
|
|
|
private func loadSubViews() {
|
|
addSubview(backView)
|
|
backView.addSubview(logoImageView)
|
|
backView.addSubview(titleLabel)
|
|
backView.addSubview(timeLabel)
|
|
|
|
translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
self.snp.makeConstraints { make in
|
|
make.size.equalTo(CGSize(width: 250, height: 250))
|
|
}
|
|
|
|
timeLabel.snp.makeConstraints { make in
|
|
make.left.right.equalToSuperview()
|
|
make.top.equalToSuperview().offset(25)
|
|
}
|
|
|
|
backView.snp.makeConstraints { make in
|
|
make.edges.equalToSuperview()
|
|
}
|
|
|
|
logoImageView.snp.makeConstraints { make in
|
|
make.size.equalTo(CGSize(width: 90, height: 72))
|
|
make.centerX.equalToSuperview()
|
|
make.top.equalToSuperview().offset(50)
|
|
}
|
|
|
|
titleLabel.snp.makeConstraints { make in
|
|
make.left.right.equalTo(backView).inset(0)
|
|
make.top.equalTo(logoImageView.snp.bottom).offset(25)
|
|
}
|
|
|
|
}
|
|
}
|