110 lines
3.3 KiB
Swift
110 lines
3.3 KiB
Swift
//
|
|
// LoaderView.swift
|
|
// Privado
|
|
//
|
|
// Created by Viktor on 29.06.2020.
|
|
// Copyright © 2020 Privado LLC. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
final class LoaderView: UIView {
|
|
|
|
static let shared = LoaderView(frame: .zero)
|
|
|
|
private var loader: UIImageView?
|
|
private var spinnerTask: DispatchWorkItem?
|
|
|
|
private var currentAnimationStep = 0
|
|
|
|
private enum Constants {
|
|
static let loaderImageName = "navigation_loader"
|
|
static let loaderSize: CGFloat = 120
|
|
|
|
// see image named above in asset (it has 6 sectors)
|
|
static let arcCount: Int = 6
|
|
}
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
self.setup()
|
|
}
|
|
|
|
@available(*, unavailable)
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
private func infinteAnimation() {
|
|
|
|
self.spinnerTask = DispatchWorkItem { [weak self] in
|
|
|
|
guard let self = self else {
|
|
return
|
|
}
|
|
|
|
self.loader?.transform = CGAffineTransform(rotationAngle: 2*CGFloat(self.currentAnimationStep%Constants.arcCount)*CGFloat.pi/CGFloat(Constants.arcCount))
|
|
self.currentAnimationStep += 1
|
|
self.infinteAnimation()
|
|
}
|
|
guard let task = self.spinnerTask else {
|
|
return
|
|
}
|
|
DispatchQueue.main.asyncAfter(deadline: .now()+0.12, execute: task)
|
|
}
|
|
|
|
func startAnimation() {
|
|
|
|
if self.loader == nil {
|
|
|
|
let image = UIImage(imageLiteralResourceName: Constants.loaderImageName)
|
|
self.loader = UIImageView(image: image)
|
|
self.loader?.translatesAutoresizingMaskIntoConstraints = false
|
|
guard let imageLoader = self.loader else {
|
|
return
|
|
}
|
|
self.addSubview(imageLoader)
|
|
NSLayoutConstraint.activate([
|
|
imageLoader.centerXAnchor.constraint(equalTo: self.centerXAnchor),
|
|
imageLoader.centerYAnchor.constraint(equalTo: self.centerYAnchor),
|
|
imageLoader.widthAnchor.constraint(lessThanOrEqualToConstant: Constants.loaderSize),
|
|
imageLoader.heightAnchor.constraint(lessThanOrEqualToConstant: Constants.loaderSize)
|
|
])
|
|
}
|
|
|
|
if self.isHidden == true {
|
|
self.spinnerTask?.cancel()
|
|
}
|
|
self.isHidden = false
|
|
self.infinteAnimation()
|
|
}
|
|
|
|
func stopAnimation() {
|
|
self.spinnerTask?.cancel()
|
|
self.isHidden = true
|
|
self.loader?.removeFromSuperview()
|
|
self.loader = nil
|
|
|
|
}
|
|
|
|
private func setup() {
|
|
self.backgroundColor = UIColor(rgb: 0x151935)
|
|
self.alpha = 0.87
|
|
self.translatesAutoresizingMaskIntoConstraints = false
|
|
self.isHidden = true
|
|
guard let window = UIApplication.shared.keyWindow else {
|
|
return
|
|
}
|
|
window.addSubview(self)
|
|
NSLayoutConstraint.activate([
|
|
self.topAnchor.constraint(equalTo: window.topAnchor),
|
|
self.bottomAnchor.constraint(equalTo: window.bottomAnchor),
|
|
self.trailingAnchor.constraint(equalTo: window.trailingAnchor),
|
|
self.leadingAnchor.constraint(equalTo: window.leadingAnchor)
|
|
])
|
|
|
|
}
|
|
|
|
}
|