Skip to content

Commit

Permalink
Added TKProgress View
Browse files Browse the repository at this point in the history
  • Loading branch information
Toseefhusen committed Feb 13, 2018
1 parent 570d940 commit 62d5210
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 7 deletions.
4 changes: 4 additions & 0 deletions TKProgressView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
15DF40A22032E51C00C64CE3 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 15DF40A02032E51C00C64CE3 /* Main.storyboard */; };
15DF40A42032E51C00C64CE3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 15DF40A32032E51C00C64CE3 /* Assets.xcassets */; };
15DF40A72032E51C00C64CE3 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 15DF40A52032E51C00C64CE3 /* LaunchScreen.storyboard */; };
15DF40AF2032E55E00C64CE3 /* TKProgressView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15DF40AE2032E55D00C64CE3 /* TKProgressView.swift */; };
/* End PBXBuildFile section */

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

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -56,6 +58,7 @@
children = (
15DF409C2032E51C00C64CE3 /* AppDelegate.swift */,
15DF409E2032E51C00C64CE3 /* ViewController.swift */,
15DF40AE2032E55D00C64CE3 /* TKProgressView.swift */,
15DF40A02032E51C00C64CE3 /* Main.storyboard */,
15DF40A32032E51C00C64CE3 /* Assets.xcassets */,
15DF40A52032E51C00C64CE3 /* LaunchScreen.storyboard */,
Expand Down Expand Up @@ -137,6 +140,7 @@
buildActionMask = 2147483647;
files = (
15DF409F2032E51C00C64CE3 /* ViewController.swift in Sources */,
15DF40AF2032E55E00C64CE3 /* TKProgressView.swift in Sources */,
15DF409D2032E51C00C64CE3 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
96 changes: 96 additions & 0 deletions TKProgressView/TKProgressView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import Foundation
import UIKit

extension UIColor {
convenience init(_ hex6: UInt32) {
let divisor = CGFloat(255)
let red = CGFloat((hex6 & 0xFF0000) >> 16) / divisor
let green = CGFloat((hex6 & 0x00FF00) >> 8) / divisor
let blue = CGFloat((hex6 & 0x0000FF) >> 0) / divisor
self.init(red: red, green: green, blue: blue, alpha: 1.0)
}
}

class TKProgressView: UIView {
var colors = [
UIColor(0xee1c27),UIColor(0xbc0271),UIColor(0x612d92),UIColor(0x283897),
UIColor(0x016db8),UIColor(0x02a2b8),UIColor(0x00a666),UIColor(0xa7d04e),
UIColor(0xfef200),UIColor(0xfeaf16),UIColor(0xf58020),UIColor(0xf35724)]

fileprivate var startIndex = 3

var isCircular = true
var numberOfBalls = 12
var speed: Double = 0.75

fileprivate var timeStep: Int = 0
fileprivate var radius: CGFloat = 0
fileprivate var displayLink: CADisplayLink?

required override init(frame: CGRect = CGRect(x: 0, y: 0, width: 100, height: 100)) {
super.init(frame: frame)

radius = min(frame.width, frame.height) / 3
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

frame.size.width = 100
frame.size.height = 100

radius = min(frame.width, frame.height) / 3
}

func startAnimating() {
displayLink = UIScreen.main.displayLink(withTarget: self, selector: #selector(TKProgressView.animateBalls))
displayLink?.add(to: .current, forMode: .defaultRunLoopMode)
}

func stopAnimating() {
displayLink?.invalidate()
timeStep = 0
}

func clear() {
layer.sublayers?.removeAll()
}

func drawBall(_ x: CGFloat, _ y: CGFloat, _ color: UIColor) {
let dot = CAShapeLayer()
dot.path = UIBezierPath(ovalIn: CGRect(x: x, y: y, width: 7, height: 7)).cgPath
dot.fillColor = color.cgColor
dot.strokeColor = UIColor.clear.cgColor
dot.lineWidth = 0
layer.addSublayer(dot)
}

func animateBalls() {
clear()

for i in startIndex ..< numberOfBalls + startIndex {
drawBall(getX(i: i, timeStep: timeStep), getY(i: i, timeStep: timeStep), colors[i-startIndex])
}

timeStep += 1
}

func getR(i: Int) -> CGFloat {
return CGFloat((CGFloat(1+i) / CGFloat(numberOfBalls)) * radius)
}

func getT(i: Int, timeStep: Int) -> Double {
return Double(timeStep) * (Double(i) / 100 * speed + 0.005)
}

func getX(i: Int, timeStep: Int) -> CGFloat {
return (frame.width / 2) + CGFloat(getR(i: i)) * cos(CGFloat(getT(i: i, timeStep: timeStep)))
}

func getY(i: Int, timeStep: Int) -> CGFloat {
if isCircular {
return (frame.height / 2) + CGFloat(getR(i: i)) * sin(CGFloat(getT(i: i, timeStep: timeStep)))
} else {
return frame.height / 2
}
}
}
10 changes: 3 additions & 7 deletions TKProgressView/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
let tkView: TKProgressView = TKProgressView.init(frame: CGRect.init(x: 50, y: 50, width: 100, height: 100 ))
self.view.addSubview(tkView)
tkView.startAnimating()
}


}

0 comments on commit 62d5210

Please sign in to comment.