Music_Player3/relax.offline.mp3.music/MP/Common/Base(公用基类)/Views/MP_Lunch_ProgressView.swift

80 lines
2.7 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.

//
// Lunch_ProgressView.swift
// MusicPlayer
//
// Created by Mr.Zhou on 2024/3/27.
//
import UIKit
class MP_Lunch_ProgressView: UIView {
//
private var gradientLayer: CAGradientLayer!
//
private var progressLayer: CALayer!
// (0.0 - 1.0)
var progress: CGFloat = 0.0 {
didSet {
updateProgressLayer()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setupGradientLayer()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupGradientLayer()
}
override func layoutSubviews() {
super.layoutSubviews()
//
gradientLayer.frame = self.bounds
updateProgressLayer()
}
private func setupGradientLayer() {
backgroundColor = .init(hex: "#FFFFFF",alpha: 0.1)
//
gradientLayer = CAGradientLayer()
gradientLayer.colors = [
UIColor(red: 0.109, green: 0.784, blue: 0.932, alpha: 1).cgColor,
UIColor(red: 0.412, green: 0.996, blue: 0.451, alpha: 1).cgColor,
UIColor(red: 0.796, green: 0.839, blue: 0.294, alpha: 1).cgColor
]
gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
layer.addSublayer(gradientLayer)
//
progressLayer = CALayer()
progressLayer.backgroundColor = UIColor.white.cgColor //
gradientLayer.mask = progressLayer
}
private func updateProgressLayer() {
//
let progressWidth = self.bounds.width * progress
progressLayer.frame = CGRect(x: 0, y: 0, width: progressWidth, height: self.bounds.height)
}
//
func setProgress(_ progress: CGFloat, animated: Bool = false) {
self.progress = min(max(progress, 0.0), 1.0) //
if animated {
let animation = CABasicAnimation(keyPath: "bounds.size.width")
animation.toValue = self.bounds.width * self.progress
animation.duration = 0.1 //
animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
animation.fillMode = .forwards
animation.isRemovedOnCompletion = false
progressLayer.add(animation, forKey: "progressWidth")
}
}
}