Music_Player3/relax.offline.mp3.music/MP/MPPositive/Views/Base/MPPositive_BottomShowView.swift
2024-12-25 10:02:00 +08:00

235 lines
9.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// MPPositive_BottomShowView.swift
// MusicPlayer
//
// Created by Mr.Zhou on 2024/4/19.
//
import UIKit
import Kingfisher
///bView
class MPPositive_BottomShowView: UIView {
//绿
private lazy var bgGreenImageView:UIImageView = UIImageView(image: .init(named: "BottomShow'bg"))
//View
private lazy var coverContentView:UIView = {
let view:UIView = .init(frame: .init(x: 26*width, y: 13*width, width: 48*width, height: 48*width))
view.backgroundColor = .clear
return view
}()
//
private lazy var coverImageView:UIImageView = {
let imageView:UIImageView = .init()
imageView.contentMode = .scaleAspectFill
imageView.layer.cornerRadius = 24*width
imageView.layer.masksToBounds = true
return imageView
}()
//Label
private lazy var titleLabel:UILabel = createLabel(font: .systemFont(ofSize: 16*width, weight: .semibold), textColor: .init(hex: "#000000"), textAlignment: .left)
////
private lazy var subtitleLabel:UILabel = createLabel(font: .systemFont(ofSize: 12*width, weight: .light), textColor: .init(hex: "#000000"), textAlignment: .left)
//ProgressView
private lazy var progressView:UIProgressView = {
let progressView:UIProgressView = .init()
progressView.isUserInteractionEnabled = false
progressView.progressTintColor = .white
progressView.trackTintColor = .clear
progressView.progress = 0
return progressView
}()
//
private lazy var nextBtn:UIButton = {
let btn = UIButton()
btn.setBackgroundImage(UIImage(named: "Bottom Next'logo"), for: .normal)
btn.addTarget(self, action: #selector(nextClick(_ :)), for: .touchUpInside)
return btn
}()
//
private lazy var playStatuBtn:UIButton = {
let btn = UIButton()
btn.setBackgroundImage(UIImage(named: "Bottom Pause'logo"), for: .normal)
btn.setBackgroundImage(UIImage(named: "Bottom Play'logo"), for: .selected)
btn.addTarget(self, action: #selector(switchStatuClick(_ :)), for: .touchUpInside)
return btn
}()
//
private var isRotating:Bool = false
//
var showListBlock:(() -> Void)?
override init(frame: CGRect) {
super.init(frame: frame)
//
NotificationCenter.notificationKey.add(observer: self, selector: #selector(currentVideoSwitchAction(_:)), notificationName: .positive_player_reload)
NotificationCenter.notificationKey.add(observer: self, selector: #selector(statusSwitchAction(_:)), notificationName: .switch_player_status)
confirgue()
//
MP_PlayerManager.shared.bottomProgressBlock = { [weak self] (currentTime, duration) in
guard let self = self else { return }
DispatchQueue.main.async {
//
let value = currentTime/duration
self.progressView.progress = Float(value)
}
}
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
override func didMoveToWindow() {
super.didMoveToWindow()
if isRotating {
addRotationAnimation()
}else {
stopRotationAnimation()
}
}
//
private func confirgue() {
addSubview(bgGreenImageView)
bgGreenImageView.snp.makeConstraints { make in
make.left.right.top.bottom.equalToSuperview()
}
addSubview(coverContentView)
coverContentView.addSubview(coverImageView)
coverImageView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
addSubview(nextBtn)
nextBtn.snp.makeConstraints { make in
make.right.equalToSuperview().offset(-24*width)
make.centerY.equalToSuperview()
make.height.width.equalTo(24*width)
}
addSubview(playStatuBtn)
playStatuBtn.snp.makeConstraints { make in
make.width.height.equalTo(24*width)
make.centerY.equalTo(nextBtn)
make.right.equalTo(nextBtn.snp.left).offset(-20*width)
}
addSubview(titleLabel)
titleLabel.snp.makeConstraints { make in
make.top.equalTo(coverImageView).offset(5*width)
make.left.equalTo(coverImageView.snp.right).offset(12*width)
make.right.equalTo(playStatuBtn.snp.left).offset(-10*width)
}
addSubview(subtitleLabel)
subtitleLabel.snp.makeConstraints { make in
make.left.equalTo(titleLabel)
make.top.equalTo(titleLabel.snp.bottom).offset(4*width)
make.right.equalTo(titleLabel)
}
addSubview(progressView)
progressView.snp.makeConstraints { make in
make.height.equalTo(4*width)
make.width.equalTo(282*width)
make.centerX.equalToSuperview()
make.top.equalToSuperview()
}
}
//
@objc private func currentVideoSwitchAction(_ sender:Notification) {
//
DispatchQueue.main.async {
[weak self] in
guard let self = self else {return}
if MP_PlayerManager.shared.loadPlayer?.currentVideo != nil {
//
coverImageView.kf.setImage(with: MP_PlayerManager.shared.loadPlayer?.currentVideo.coverUrl, placeholder: placeholderImage)
titleLabel.text = MP_PlayerManager.shared.loadPlayer?.currentVideo?.title ?? ""
subtitleLabel.text = MP_PlayerManager.shared.loadPlayer?.currentVideo?.subtitle ?? ""
progressView.progress = 0
}
}
}
//
@objc private func statusSwitchAction(_ sender:Notification) {
guard let state:MP_PlayerStateType = sender.object as? MP_PlayerStateType else {
return
}
DispatchQueue.main.async {
[weak self] in
guard let self = self else {return}
switch state {
case .Playing:
playStatuBtn.isSelected = true
if isRotating == false {
addRotationAnimation()
}
default:
playStatuBtn.isSelected = false
if isRotating == true {
stopRotationAnimation()
}
}
}
}
//
@objc private func nextClick(_ sender:UIButton) {
// guard MP_NetWorkManager.shared.netWorkStatu == .reachable else {
// MP_HUD.text("Bad connection~".localizableString(), delay: 2.0, completion: nil)
// return
// }
guard MP_PlayerManager.shared.loadPlayer?.currentVideo != nil else {
return
}
MPPositive_Debouncer.shared.call {
[weak self] in
guard let self = self else {return}
progressView.progress = 0
MP_PlayerManager.shared.nextEvent()
}
}
//
@objc private func switchStatuClick(_ sender:UIButton) {
guard MP_PlayerManager.shared.loadPlayer != nil else {
return
}
//
switch MP_PlayerManager.shared.getPlayState() {
case .Null:
//
MP_PlayerManager.shared.play()
case .Playing:
//
MP_PlayerManager.shared.pause {
[weak self] in
}
case .Pause:
//
MP_PlayerManager.shared.resume {
[weak self] in
}
}
}
//Layer
private func addRotationAnimation() {
isRotating = true
DispatchQueue.main.asyncAfter(deadline: .now()) {[weak self] in
guard let self = self, coverImageView.window != nil else {return}
coverContentView.layer.removeAnimation(forKey: "rotationAnimation")
let rotation = CABasicAnimation(keyPath: "transform.rotation.z")
rotation.fromValue = 0
rotation.toValue = Double.pi * 2
rotation.duration = 8 //
rotation.repeatCount = Float.infinity //
coverContentView.layer.add(rotation, forKey: "rotationAnimation")
}
}
//Layer
private func stopRotationAnimation() {
isRotating = false //
DispatchQueue.main.asyncAfter(deadline: .now()) {[weak self] in
guard let self = self else {return}
coverContentView.layer.removeAnimation(forKey: "rotationAnimation")
coverContentView.layer.transform = CATransform3DIdentity
}
}
}