-
Notifications
You must be signed in to change notification settings - Fork 205
/
JXPhotoBrowserProgressView.swift
79 lines (67 loc) · 2.41 KB
/
JXPhotoBrowserProgressView.swift
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
//
// JXPhotoBrowserProgressView.swift
// JXPhotoBrowser
//
// Created by JiongXing on 2018/10/14.
//
import UIKit
/// 加载进度环
open class JXPhotoBrowserProgressView: UIView {
/// 进度
open var progress: CGFloat = 0 {
didSet {
DispatchQueue.main.async {
self.fanshapedLayer.path = self.makeProgressPath(self.progress).cgPath
if self.progress >= 1.0 || self.progress < 0.01 {
self.isHidden = true
} else {
self.isHidden = false
}
}
}
}
/// 外边界
private var circleLayer: CAShapeLayer!
/// 扇形区
private var fanshapedLayer: CAShapeLayer!
public override init(frame: CGRect) {
super.init(frame: frame)
if self.frame.size.equalTo(.zero) {
self.frame.size = CGSize(width: 50, height: 50)
}
setupUI()
progress = 0
}
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupUI() {
backgroundColor = UIColor.clear
let strokeColor = UIColor(white: 1, alpha: 0.8).cgColor
circleLayer = CAShapeLayer()
circleLayer.strokeColor = strokeColor
circleLayer.fillColor = UIColor.clear.cgColor
circleLayer.path = makeCirclePath().cgPath
layer.addSublayer(circleLayer)
fanshapedLayer = CAShapeLayer()
fanshapedLayer.fillColor = strokeColor
layer.addSublayer(fanshapedLayer)
}
private func makeCirclePath() -> UIBezierPath {
let arcCenter = CGPoint(x: bounds.midX, y: bounds.midY)
let path = UIBezierPath(arcCenter: arcCenter, radius: 25, startAngle: 0, endAngle: CGFloat.pi * 2, clockwise: true)
path.lineWidth = 2
return path
}
private func makeProgressPath(_ progress: CGFloat) -> UIBezierPath {
let center = CGPoint(x: bounds.midX, y: bounds.midY)
let radius = bounds.midY - 2.5
let path = UIBezierPath()
path.move(to: center)
path.addLine(to: CGPoint(x: bounds.midX, y: center.y - radius))
path.addArc(withCenter: center, radius: radius, startAngle: -CGFloat.pi / 2, endAngle: -CGFloat.pi / 2 + CGFloat.pi * 2 * progress, clockwise: true)
path.close()
path.lineWidth = 1
return path
}
}