ActivityIndicator.swift
2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// ActivityIndicator.swift
// Pro-Seecurity-VPN
//
// Created by user247686 on 12/11/23.
//
import Foundation
import Foundation
import UIKit
class ActivityIndicator: UIView {
//in px
var lineWidth: CGFloat = 3 {
didSet {
setNeedsDisplay()
}
}
var rotationAngle: CGFloat = 0
private var forceStopAnimation = false {
didSet {
isHidden = forceStopAnimation
}
}
private var isRotating = false
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
private func setup() {
isHidden = true
backgroundColor = .clear
}
func startAnimating() {
if !isRotating {
forceStopAnimation = false
//возвращаем в исходную
rotationAngle = 0
self.transform = .identity
rotate()
}
}
func stopAnimating() {
forceStopAnimation = true
}
private func rotate() {
isRotating = true
self.rotationAngle += 90
UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveLinear, animations: {
self.transform = CGAffineTransform(rotationAngle: self.rotationAngle.degreesToRadians)
}) { finished in
if !self.forceStopAnimation {
self.rotate()
} else {
self.isRotating = false
}
}
}
override func draw(_ rect: CGRect) {
let center = CGPoint(x: bounds.width / 2, y: bounds.height / 2)
let radius: CGFloat = max(bounds.width, bounds.height)
//bottom line
var startAngle: CGFloat = 0
var endAngle: CGFloat = 360
var path = UIBezierPath(arcCenter: center,
radius: radius/2-lineWidth,
startAngle: startAngle.degreesToRadians,
endAngle: endAngle.degreesToRadians,
clockwise: true)
path.lineWidth = lineWidth
UIColor.clear.setStroke()
path.stroke()
startAngle = 270
endAngle = 540
path = UIBezierPath(arcCenter: center,
radius: radius/2-lineWidth,
startAngle: startAngle.degreesToRadians,
endAngle: endAngle.degreesToRadians,
clockwise: true)
path.lineWidth = lineWidth
UIColor.white.setStroke()
path.stroke()
}
}