222 lines
6.9 KiB
Swift
222 lines
6.9 KiB
Swift
//
|
|
// HomeVoicePublishView.swift
|
|
// yinmeng-ios
|
|
//
|
|
// Created by yinmeng on 2024/2/29.
|
|
//
|
|
|
|
import UIKit
|
|
import Nuke
|
|
|
|
|
|
typealias ClickPublishBlock = ()->Void
|
|
typealias PlayVoiceEndBlock = ()->Void
|
|
|
|
|
|
class HomeVoicePublishView: UIView {
|
|
var clickPublishBlock:ClickPublishBlock?
|
|
var playVoiceEndBlock:PlayVoiceEndBlock?
|
|
|
|
var currentAngle: CGFloat = 0.0
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
setUILayout()
|
|
loadSVGA()
|
|
|
|
let tap = UITapGestureRecognizer(target: self, action: #selector(publishViewAction))
|
|
publishView.addGestureRecognizer(tap)
|
|
}
|
|
|
|
func setUILayout(){
|
|
addSubview(backgroundView)
|
|
addSubview(discView)
|
|
addSubview(avatarView)
|
|
addSubview(playBtn)
|
|
addSubview(resetBtn)
|
|
addSubview(publishView)
|
|
|
|
backgroundView.snp.makeConstraints { make in
|
|
make.edges.equalTo(self)
|
|
}
|
|
|
|
discView.snp.makeConstraints { make in
|
|
make.left.equalTo(8)
|
|
make.top.equalTo(4)
|
|
make.width.height.equalTo(136)
|
|
}
|
|
avatarView.snp.makeConstraints { make in
|
|
make.width.height.equalTo(58)
|
|
make.center.equalTo(self.discView)
|
|
}
|
|
|
|
resetBtn.snp.makeConstraints { make in
|
|
make.width.height.equalTo(44)
|
|
make.right.equalTo(-44)
|
|
make.top.equalTo(18)
|
|
}
|
|
playBtn.snp.makeConstraints { make in
|
|
make.right.equalTo(self.resetBtn.snp.left).offset(-32)
|
|
make.top.equalTo(18)
|
|
make.width.height.equalTo(44)
|
|
}
|
|
publishView.snp.makeConstraints { make in
|
|
make.top.equalTo(74)
|
|
make.width.equalTo(168)
|
|
make.height.equalTo(52)
|
|
make.right.equalTo(-20)
|
|
}
|
|
}
|
|
|
|
func loadSVGA() {
|
|
discParser.parse(withNamed: "RecordPlayer", in: Bundle.main) {[weak self] videoItem in
|
|
guard let self else {
|
|
return
|
|
}
|
|
self.discView.videoItem = videoItem
|
|
self.discView.loops = 100000;
|
|
self.discView.clearsAfterStop = false
|
|
} failureBlock: { error in
|
|
|
|
}
|
|
publishParser.parse(withNamed: "PublishVoice", in: Bundle.main) {[weak self] videoItem in
|
|
guard let self else {
|
|
return
|
|
}
|
|
self.publishView.videoItem = videoItem
|
|
self.publishView.loops = 100000;
|
|
self.publishView.startAnimation()
|
|
} failureBlock: { error in
|
|
|
|
}
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
@objc func publishViewAction(){
|
|
guard let _clickPublishBlock = self.clickPublishBlock else {
|
|
return
|
|
}
|
|
_clickPublishBlock()
|
|
if playBtn.isSelected {
|
|
discView.stopAnimation()
|
|
playBtn.isSelected = false
|
|
PlayVoiceManager.shared.stopPlayVoice()
|
|
}
|
|
}
|
|
|
|
func playAvatarAnimation() {
|
|
let rotation = CABasicAnimation(keyPath: "transform.rotation")
|
|
rotation.fromValue = currentAngle
|
|
rotation.toValue = currentAngle + Double.pi * 2
|
|
rotation.duration = 4
|
|
rotation.repeatCount = Float.infinity
|
|
avatarView.layer.add(rotation, forKey: "rotationAnimation")
|
|
}
|
|
|
|
func stopAvatarAnimation() {
|
|
if let presentationLayer = avatarView.layer.presentation() {
|
|
let currentTransform = presentationLayer.transform
|
|
// 移除动画
|
|
avatarView.layer.removeAnimation(forKey: "rotationAnimation")
|
|
// 应用当前状态
|
|
avatarView.layer.transform = currentTransform
|
|
currentAngle = atan2(currentTransform.m12, currentTransform.m11)
|
|
} else {
|
|
avatarView.layer.removeAnimation(forKey: "rotationAnimation")
|
|
}
|
|
}
|
|
|
|
@objc func playBtnAction(){
|
|
guard let _model = self.model else {
|
|
HUDTool.show(with: "把头像拖至唱片机才可播放")
|
|
return
|
|
}
|
|
self.playBtn.isSelected = !self.playBtn.isSelected
|
|
if (self.playBtn.isSelected){
|
|
discView.startAnimation()
|
|
PlayVoiceManager.shared.downloadVoice(url: _model.userVoice)
|
|
playAvatarAnimation()
|
|
return
|
|
}
|
|
discView.stopAnimation()
|
|
stopAvatarAnimation()
|
|
PlayVoiceManager.shared.stopPlayVoice()
|
|
}
|
|
|
|
@objc func resetBtnAction(){
|
|
avatarView.image = nil
|
|
model = nil
|
|
discView.stopAnimation()
|
|
stopAvatarAnimation()
|
|
PlayVoiceManager.shared.stopPlayVoice()
|
|
}
|
|
|
|
var backgroundView:UIImageView = {
|
|
let _backgroundView = UIImageView()
|
|
_backgroundView.image = UIImage(named: "yin_home_voice_publish_bg")
|
|
_backgroundView.isUserInteractionEnabled = true
|
|
return _backgroundView
|
|
}()
|
|
lazy var discView:SVGAImageView = {
|
|
let _discView = SVGAImageView()
|
|
_discView.contentMode = .scaleAspectFill
|
|
return _discView
|
|
}()
|
|
private lazy var discParser:SVGAParser = {
|
|
let _discParser = SVGAParser()
|
|
|
|
return _discParser
|
|
}()
|
|
private lazy var publishView:SVGAImageView = {
|
|
let _publishView = SVGAImageView()
|
|
_publishView.contentMode = .scaleAspectFill
|
|
_publishView.isUserInteractionEnabled = true
|
|
|
|
return _publishView
|
|
}()
|
|
private lazy var publishParser:SVGAParser = {
|
|
let _publishParser = SVGAParser()
|
|
|
|
return _publishParser
|
|
}()
|
|
|
|
var resetBtn:UIButton = {
|
|
let _resetBtn = UIButton()
|
|
_resetBtn.setImage(UIImage(named: "yin_hoem_voice_publis_reset"), for: .normal)
|
|
_resetBtn.addTarget(self, action: #selector(resetBtnAction), for: .touchUpInside)
|
|
return _resetBtn
|
|
}()
|
|
var playBtn:UIButton = {
|
|
let _playBtn = UIButton()
|
|
_playBtn.setImage(UIImage(named: "yin_hoem_voice_publis_play"), for: .selected)
|
|
_playBtn.setImage(UIImage(named: "yin_hoem_voice_publis_no_play"), for: .normal)
|
|
_playBtn.addTarget(self, action: #selector(playBtnAction), for: .touchUpInside)
|
|
return _playBtn
|
|
}()
|
|
var avatarView:UIImageView = {
|
|
let _avatarView = UIImageView()
|
|
|
|
_avatarView.isUserInteractionEnabled = true
|
|
_avatarView.layer.cornerRadius = 58/2;
|
|
_avatarView.layer.masksToBounds = true
|
|
_avatarView.layer.borderWidth = 2
|
|
_avatarView.layer.borderColor = UIColor.white.cgColor
|
|
return _avatarView
|
|
}()
|
|
var model:HomeVoiceModel? = nil{
|
|
didSet{
|
|
guard let _model = model else {
|
|
return
|
|
}
|
|
Nuke.loadImage(with: _model.avatar, into:avatarView)
|
|
discView.startAnimation()
|
|
playAvatarAnimation()
|
|
PlayVoiceManager.shared.downloadVoice(url: _model.userVoice)
|
|
playBtn.isSelected = true
|
|
}
|
|
}
|
|
}
|