123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- //
- // PQLoadingHUB.swift
- // PQSpeed
- //
- // Created by SanW on 2020/6/5.
- // Copyright © 2020 BytesFlow. All rights reserved.
- //
- import UIKit
- public class PQLoadingHUBView: UIView {
- // gif每一帧图
- public var gifImages: [UIImage]?
- // gif播放时长
- public var duration: Double?
- public lazy var loadingImage: UIImageView = {
- let loadingImage = UIImageView()
- loadingImage.tintColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
- return loadingImage
- }()
- override public init(frame: CGRect) {
- super.init(frame: frame)
- addSubview(loadingImage)
- isUserInteractionEnabled = false
- let data = try? Data(contentsOf: URL(fileURLWithPath: Bundle().BF_mainbundle().path(forResource: "stuckPoint_music_loading", ofType: ".gif")!))
- if data != nil {
- PQPHAssetVideoParaseUtil.parasGIFImage(data: data!, isRenderingColor: UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)) { [weak self] _, images, duration in
- self?.gifImages = images
- self?.duration = duration
- }
- }
- }
- required init?(coder _: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- override public func layoutSubviews() {
- super.layoutSubviews()
- // 334 * 307
- let imageW: CGFloat = 67
- let imageH: CGFloat = 67
- loadingImage.frame = CGRect(x: (frame.width - imageW) / 2, y: (frame.height - imageW) / 2, width: imageW, height: imageH)
- }
- /// 开始加载
- public func loading() {
- loadingImage.displayGIF(data: nil, images: gifImages, repeatCount: .max, duration: duration ?? 2)
- }
- /// 停止加载
- public func endLoading() {
- loadingImage.removePlayGIF()
- }
- override public func removeFromSuperview() {
- loadingImage.removePlayGIF()
- loadingImage.removeFromSuperview()
- }
- }
- public class PQLoadingHUB: NSObject {
- public static let shared = PQLoadingHUB()
- public let viewTag = 11111
- public var isLoading: Bool = false
- public func showHUB(isMode:Bool = false) {
- DispatchQueue.main.async { [weak self] in
- let window = UIApplication.shared.keyWindow
- if (window?.viewWithTag(self!.viewTag)) == nil {
- let loadingHUB: PQLoadingHUBView = PQLoadingHUBView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
-
- if(isMode){
- let backView = UIImageView.init(frame: window?.frame ?? .zero)
- backView.backgroundColor = .clear
- backView.isUserInteractionEnabled = true
- backView.addSubview(loadingHUB)
- backView.tag = self!.viewTag
- window?.addSubview(backView)
- }else{
- loadingHUB.tag = self!.viewTag
- window?.addSubview(loadingHUB)
- }
-
- loadingHUB.center = window?.center as! CGPoint
- loadingHUB.loading()
- self?.isLoading = true
- }
- }
- }
- public func dismissHUB() {
- DispatchQueue.main.async { [weak self] in
- let window = UIApplication.shared.keyWindow
- if (window?.viewWithTag(self!.viewTag)) != nil {
- window?.viewWithTag(self!.viewTag)?.removeFromSuperview()
- self?.isLoading = false
- }
- }
- }
- public func showHUB(superView: UIView, isVerticality: Bool = false) {
- DispatchQueue.main.async { [weak self] in
- if superView.viewWithTag(self!.viewTag) == nil {
- let hubW: CGFloat = 100
- let supW: CGFloat = superView.frame.width
- let supH: CGFloat = superView.frame.height
- let hubY: CGFloat = isVerticality ? ((supW - hubW) / 2) : ((supH - hubW) / 2)
- let hubX: CGFloat = isVerticality ? ((supH - hubW) / 2) : ((supW - hubW) / 2)
- let loadingHUB: PQLoadingHUBView = PQLoadingHUBView(frame: CGRect(x: hubX, y: hubY, width: 100, height: 100))
- loadingHUB.tag = self!.viewTag
- superView.addSubview(loadingHUB)
- loadingHUB.loading()
- self?.isLoading = true
- }
- }
- }
- public func dismissHUB(superView: UIView) {
- DispatchQueue.main.async { [weak self] in
- if let v = superView.viewWithTag(self?.viewTag ?? 999579) {
- v.removeFromSuperview()
- self?.isLoading = false
- }
- }
- }
- override private init() {
- super.init()
- }
- override public func copy() -> Any {
- return self
- }
- override public func mutableCopy() -> Any {
- return self
- }
- }
|