/
SRActivityIndicator.swift
158 lines (137 loc) · 6.02 KB
/
SRActivityIndicator.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// SR_ActivityIndicator.swift
// SR_Activity_Indicator
//
// Created by Saroj on 4/7/16.
// Copyright © 2016 Saroj. All rights reserved.
///
import UIKit
public enum maskType {
case Dim
case Blur
}
public let SRActivityIndicator = SR_ActivityIndicator()
public class SR_ActivityIndicator: UIView {
let contentView = UIView()
public var outerFillColor : UIColor = UIColor.clear
public var outerStrokeColor : UIColor = UIColor.clear
public var outerLineWidth : CGFloat = 5.0
public var outerEndStroke : CGFloat = 1.0
public var outerAnimationDuration : CGFloat = 2.0
public var enableInnerLayer : Bool = true
public var innerFillColor : UIColor = UIColor.clear
public var innerStrokeColor : UIColor = UIColor(red: 208/255, green: 154/255, blue: 35/255, alpha: 1)
public var centerImageSize: CGFloat = 50
public var centerImage: UIImage? = UIImage(named: "image")
public var innerLineWidth : CGFloat = 5.0
public var innerEndStroke : CGFloat = 0.5
public var innerAnimationDuration : CGFloat = 1.0
public var currentInnerRotation : CGFloat = 0
public var currentOuterRotation : CGFloat = 0
public var masking:maskType = .Dim
public var innerView : UIView = UIView()
public var outerView : UIView = UIView()
public var centerView : UIImageView = UIImageView()
public var disableUserInteraction = true
let vibrancyEffectView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffect.Style.dark))
//MARK:- init
override init(frame: CGRect) {
super.init(frame: frame)
self.commonInit()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self.commonInit()
}
public func setMaskingWith(maskType:maskType? = .Dim, alpha:CGFloat? = 1){
switch maskType {
case .Dim:
self.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: alpha!)
case .Blur:
vibrancyEffectView.frame = self.bounds
vibrancyEffectView.alpha = alpha!
vibrancyEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.insertSubview(vibrancyEffectView, belowSubview: self.contentView)
case .none:
break
}
}
private func commonInit(){
self.frame = UIScreen.main.bounds
if !disableUserInteraction{
self.isUserInteractionEnabled = false
}
contentView.frame = CGRect(x: 0, y: 0, width: 25+centerImageSize, height: 25+centerImageSize)
self.addSubview(contentView)
contentView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
contentView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
}
//MARK:- draw
override public func draw(_ rect: CGRect) {
contentView.frame = CGRect(x: 0, y: 0, width: 25+centerImageSize, height: 25+centerImageSize)
self.contentView.addSubview(outerView)
outerView.frame = CGRect(x: 0 , y: 0, width: contentView.frame.size.width, height: contentView.frame.size.height)
outerView.center = self.convert(self.center, from: self.contentView)
let outerLayer = CAShapeLayer()
outerLayer.path = UIBezierPath(ovalIn: outerView.bounds).cgPath
outerLayer.lineWidth = outerLineWidth
outerLayer.strokeStart = 0.0
outerLayer.strokeEnd = outerEndStroke
outerLayer.fillColor = outerFillColor.cgColor
outerLayer.strokeColor = outerStrokeColor.cgColor
outerView.layer.addSublayer(outerLayer)
self.contentView.addSubview(centerView)
centerView.frame = CGRect(x: 0, y: 0, width: centerImageSize, height: centerImageSize)
centerView.layer.cornerRadius = centerView.frame.width / 2
centerView.clipsToBounds = true
centerView.center = self.convert(self.center, from: self.contentView)
centerView.image = centerImage
if enableInnerLayer{
self.contentView.addSubview(innerView)
innerView.frame = CGRect(x: 0 , y: 0, width: contentView.frame.size.width , height: contentView.frame.size.height)
innerView.center = self.convert(self.center, from: self.contentView)
let innerLayer = CAShapeLayer()
innerLayer.path = UIBezierPath(ovalIn: innerView.bounds).cgPath
innerLayer.lineWidth = innerLineWidth
innerLayer.strokeStart = 0
innerLayer.strokeEnd = innerEndStroke
//innerLayer.lineCap = CAShapeLayerLineCap.round
innerLayer.fillColor = innerFillColor.cgColor
innerLayer.strokeColor = innerStrokeColor.cgColor
innerView.layer.addSublayer(innerLayer)
}
}
//MARK:- Public
public func animateInnerRing(){
innerView.layer.removeAllAnimations()
let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotationAnimation.fromValue = 0 * CGFloat(Double.pi/180)
rotationAnimation.toValue = 360 * CGFloat(Double.pi/180)
rotationAnimation.duration = Double(innerAnimationDuration)
rotationAnimation.repeatCount = HUGE
self.innerView.layer.add(rotationAnimation, forKey: "rotateInner")
}
public func startAnimating(){
self.animateInnerRing()
}
public func show() {
DispatchQueue.main.async {
self.removeFromSuperview()
let appDelegate = UIApplication.shared.delegate
appDelegate!.window??.addSubview(self)
self.alpha = 0
UIView.animate(withDuration: 0.3, animations: {
self.alpha = 1
}, completion: { (true) in
self.animateInnerRing()
})
}
}
public func dissmiss(){
UIView.animate(withDuration: 0.3,animations: {() -> Void in
self.alpha = 0
}, completion: { _ in
self.innerView.layer.removeAllAnimations()
})
}
}