Music_Player3/relax.offline.mp3.music/MP/MPPositive/Views/Player/MPPositive_PlayerSilder.swift

147 lines
5.5 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_PlayerSilder.swift
// MusicPlayer
//
// Created by Mr.Zhou on 2024/5/8.
//
import UIKit
///b
class MPPositive_PlayerSilder: UISlider {
///
var hitTestEdgeInsets: UIEdgeInsets = UIEdgeInsets(top: -20, left: -20, bottom: -20, right: -20)
/// Slider
var originalFrame: CGRect?
//
var thumbImage:UIImage = .init(named: "Player_Slider'logo")!
//
var trackHeight: CGFloat = 6*width
//
var minTrackColors: [UIColor]!
//
var maxTrackColors:[UIColor]!
//()
var minTrackLocations:[CGFloat]!
//()
var maxTrackLocations:[CGFloat]!
//
override init(frame:CGRect) {
super.init(frame: frame)
setUpLayout()
}
override func awakeFromNib() {
super.awakeFromNib()
print("调整Slider大小")
originalFrame = self.frame
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
private func setUpLayout() {
layer.masksToBounds = false
setThumbImage(thumbImage, for: .normal)
//
let minTrackImg = makeTrackImage(rect: frame, colors: [UIColor.white], locations: [0,1])
setMinimumTrackImage(minTrackImg, for: .normal)
//
let maxTrackImg = makeTrackImage(rect: frame, colors: [UIColor(hex: "#FFFFFF", alpha: 0.1)], locations: [0,1])
setMaximumTrackImage(maxTrackImg, for: .normal)
}
///
/// - Parameters:
/// - rect:
/// - color:
/// - Returns:
private func makeThumbImage(rect: CGRect, color:UIColor) -> UIImage {
let lineWidth: CGFloat = 2
//
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
//
color.setFill()
//线
let oval = UIBezierPath(ovalIn: rect)
//
oval.fill()
//
UIColor.white.setStroke()
//
oval.lineWidth = lineWidth
oval.stroke()
//
let thumbImg = UIGraphicsGetImageFromCurrentImageContext()!
//
UIGraphicsEndImageContext()
return thumbImg
}
///
/// - Parameters:
/// - rect:
/// - colors:
/// - locations:
/// - Returns:
private func makeTrackImage(rect: CGRect, colors:[UIColor], locations:[CGFloat]) -> UIImage {
let rect = CGRect(x: rect.minX, y: rect.minY, width: rect.width, height: self.trackHeight)
//
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
let ctx = UIGraphicsGetCurrentContext()!
//
let cornerRadius: CGFloat = rect.height * 0.5
let path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius).cgPath
//
ctx.addPath(path)
ctx.clip()
// 使rgb
let colorSpace = CGColorSpaceCreateDeviceRGB()
//cgcolors
var cgColors:[CGColor] = []
colors.forEach { item in
cgColors.append(item.cgColor)
}
//
let gradient:CGGradient = CGGradient(colorsSpace: colorSpace, colors: cgColors as CFArray, locations: locations)!
//
let start = CGPoint(x: self.bounds.minX, y: self.bounds.minY)
//
let end = CGPoint(x: self.bounds.maxX, y: self.bounds.minY)
//
ctx.drawLinearGradient(gradient, start: start, end: end, options: .drawsBeforeStartLocation)
//
let trackImg = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return trackImg
}
// thumb
override func thumbRect(forBounds bounds: CGRect, trackRect rect: CGRect, value: Float) -> CGRect {
let rect = super.thumbRect(forBounds: bounds, trackRect: rect, value: value)
// thumb
// (setValue(_:animated:)animatedtruethumb)
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOffset = CGSize(width: 0, height: 0)
self.layer.shadowOpacity = 0.15
self.layer.shadowRadius = 3
self.layer.shadowPath = UIBezierPath(cgPath: CGPath(ellipseIn: rect.insetBy(dx: 3, dy: 3), transform: nil)).cgPath
return rect
}
// track
override func trackRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: 0, y: 0, width: bounds.width, height: self.trackHeight)
}
//MARK: -
//
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
//Slider
let largerFrame: CGRect = self.bounds.inset(by: hitTestEdgeInsets)
//
let isInside = largerFrame.contains(point)
return true
}
//slider
}