Skip to content

Commit 62d5210

Browse files
author
Toseefhusen
committed
Added TKProgress View
1 parent 570d940 commit 62d5210

File tree

3 files changed

+103
-7
lines changed

3 files changed

+103
-7
lines changed

TKProgressView.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
15DF40A22032E51C00C64CE3 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 15DF40A02032E51C00C64CE3 /* Main.storyboard */; };
1313
15DF40A42032E51C00C64CE3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 15DF40A32032E51C00C64CE3 /* Assets.xcassets */; };
1414
15DF40A72032E51C00C64CE3 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 15DF40A52032E51C00C64CE3 /* LaunchScreen.storyboard */; };
15+
15DF40AF2032E55E00C64CE3 /* TKProgressView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15DF40AE2032E55D00C64CE3 /* TKProgressView.swift */; };
1516
/* End PBXBuildFile section */
1617

1718
/* Begin PBXFileReference section */
@@ -22,6 +23,7 @@
2223
15DF40A32032E51C00C64CE3 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
2324
15DF40A62032E51C00C64CE3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
2425
15DF40A82032E51C00C64CE3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
26+
15DF40AE2032E55D00C64CE3 /* TKProgressView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TKProgressView.swift; sourceTree = "<group>"; };
2527
/* End PBXFileReference section */
2628

2729
/* Begin PBXFrameworksBuildPhase section */
@@ -56,6 +58,7 @@
5658
children = (
5759
15DF409C2032E51C00C64CE3 /* AppDelegate.swift */,
5860
15DF409E2032E51C00C64CE3 /* ViewController.swift */,
61+
15DF40AE2032E55D00C64CE3 /* TKProgressView.swift */,
5962
15DF40A02032E51C00C64CE3 /* Main.storyboard */,
6063
15DF40A32032E51C00C64CE3 /* Assets.xcassets */,
6164
15DF40A52032E51C00C64CE3 /* LaunchScreen.storyboard */,
@@ -137,6 +140,7 @@
137140
buildActionMask = 2147483647;
138141
files = (
139142
15DF409F2032E51C00C64CE3 /* ViewController.swift in Sources */,
143+
15DF40AF2032E55E00C64CE3 /* TKProgressView.swift in Sources */,
140144
15DF409D2032E51C00C64CE3 /* AppDelegate.swift in Sources */,
141145
);
142146
runOnlyForDeploymentPostprocessing = 0;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import Foundation
2+
import UIKit
3+
4+
extension UIColor {
5+
convenience init(_ hex6: UInt32) {
6+
let divisor = CGFloat(255)
7+
let red = CGFloat((hex6 & 0xFF0000) >> 16) / divisor
8+
let green = CGFloat((hex6 & 0x00FF00) >> 8) / divisor
9+
let blue = CGFloat((hex6 & 0x0000FF) >> 0) / divisor
10+
self.init(red: red, green: green, blue: blue, alpha: 1.0)
11+
}
12+
}
13+
14+
class TKProgressView: UIView {
15+
var colors = [
16+
UIColor(0xee1c27),UIColor(0xbc0271),UIColor(0x612d92),UIColor(0x283897),
17+
UIColor(0x016db8),UIColor(0x02a2b8),UIColor(0x00a666),UIColor(0xa7d04e),
18+
UIColor(0xfef200),UIColor(0xfeaf16),UIColor(0xf58020),UIColor(0xf35724)]
19+
20+
fileprivate var startIndex = 3
21+
22+
var isCircular = true
23+
var numberOfBalls = 12
24+
var speed: Double = 0.75
25+
26+
fileprivate var timeStep: Int = 0
27+
fileprivate var radius: CGFloat = 0
28+
fileprivate var displayLink: CADisplayLink?
29+
30+
required override init(frame: CGRect = CGRect(x: 0, y: 0, width: 100, height: 100)) {
31+
super.init(frame: frame)
32+
33+
radius = min(frame.width, frame.height) / 3
34+
}
35+
required init?(coder aDecoder: NSCoder) {
36+
super.init(coder: aDecoder)
37+
38+
frame.size.width = 100
39+
frame.size.height = 100
40+
41+
radius = min(frame.width, frame.height) / 3
42+
}
43+
44+
func startAnimating() {
45+
displayLink = UIScreen.main.displayLink(withTarget: self, selector: #selector(TKProgressView.animateBalls))
46+
displayLink?.add(to: .current, forMode: .defaultRunLoopMode)
47+
}
48+
49+
func stopAnimating() {
50+
displayLink?.invalidate()
51+
timeStep = 0
52+
}
53+
54+
func clear() {
55+
layer.sublayers?.removeAll()
56+
}
57+
58+
func drawBall(_ x: CGFloat, _ y: CGFloat, _ color: UIColor) {
59+
let dot = CAShapeLayer()
60+
dot.path = UIBezierPath(ovalIn: CGRect(x: x, y: y, width: 7, height: 7)).cgPath
61+
dot.fillColor = color.cgColor
62+
dot.strokeColor = UIColor.clear.cgColor
63+
dot.lineWidth = 0
64+
layer.addSublayer(dot)
65+
}
66+
67+
func animateBalls() {
68+
clear()
69+
70+
for i in startIndex ..< numberOfBalls + startIndex {
71+
drawBall(getX(i: i, timeStep: timeStep), getY(i: i, timeStep: timeStep), colors[i-startIndex])
72+
}
73+
74+
timeStep += 1
75+
}
76+
77+
func getR(i: Int) -> CGFloat {
78+
return CGFloat((CGFloat(1+i) / CGFloat(numberOfBalls)) * radius)
79+
}
80+
81+
func getT(i: Int, timeStep: Int) -> Double {
82+
return Double(timeStep) * (Double(i) / 100 * speed + 0.005)
83+
}
84+
85+
func getX(i: Int, timeStep: Int) -> CGFloat {
86+
return (frame.width / 2) + CGFloat(getR(i: i)) * cos(CGFloat(getT(i: i, timeStep: timeStep)))
87+
}
88+
89+
func getY(i: Int, timeStep: Int) -> CGFloat {
90+
if isCircular {
91+
return (frame.height / 2) + CGFloat(getR(i: i)) * sin(CGFloat(getT(i: i, timeStep: timeStep)))
92+
} else {
93+
return frame.height / 2
94+
}
95+
}
96+
}

TKProgressView/ViewController.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@ class ViewController: UIViewController {
1212

1313
override func viewDidLoad() {
1414
super.viewDidLoad()
15-
// Do any additional setup after loading the view, typically from a nib.
16-
}
1715

18-
override func didReceiveMemoryWarning() {
19-
super.didReceiveMemoryWarning()
20-
// Dispose of any resources that can be recreated.
16+
let tkView: TKProgressView = TKProgressView.init(frame: CGRect.init(x: 50, y: 50, width: 100, height: 100 ))
17+
self.view.addSubview(tkView)
18+
tkView.startAnimating()
2119
}
22-
23-
2420
}
2521

0 commit comments

Comments
 (0)