Skip to content

Instantly share code, notes, and snippets.

@jhays
Created October 6, 2017 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhays/ce5dc57403dcffeec15991e39b2fd99f to your computer and use it in GitHub Desktop.
Save jhays/ce5dc57403dcffeec15991e39b2fd99f to your computer and use it in GitHub Desktop.
JHSpinner Swift Playground
import UIKit
import PlaygroundSupport
class JHSpinner : UIView {
private let containerView = UIView()
private let line1 = UIView()
private let line2 = UIView()
private let line3 = UIView()
private let lineWidth = CGFloat(6.0)
private let lineHeight = CGFloat(6.0)
private let lineMax = CGFloat(26.0)
private let xMargin = CGFloat(4.0)
private let animationTime = 0.15
private var midX = CGFloat(0)
private var midY = CGFloat(0)
private var animationCounter = 0
override init(frame: CGRect) {
super.init(frame: frame)
self.containerView.frame = frame
addSubview(containerView)
setup()
}
required init?(coder:NSCoder) {
super.init(coder: coder)
}
private func setup() {
midX = containerView.frame.width/2
midY = containerView.frame.height/2
line1.frame = CGRect(x: midX - ((lineWidth / 2) + lineWidth + xMargin), y: midY - (lineHeight/2), width: lineWidth, height: lineHeight)
line1.backgroundColor = .red
line1.layer.cornerRadius = lineWidth/2.0
containerView.addSubview(line1)
line2.frame = CGRect(x: CGFloat(midX - (lineWidth / 2)), y: midY - (lineHeight/2), width: lineWidth, height: lineHeight)
line2.backgroundColor = .red
line2.layer.cornerRadius = lineWidth/2.0
containerView.addSubview(line2)
line3.frame = CGRect(x: midX + ((lineWidth / 2) + xMargin), y: midY - (lineHeight/2), width: lineWidth, height: lineHeight)
line3.backgroundColor = .red
line3.layer.cornerRadius = lineWidth/2.0
containerView.addSubview(line3)
}
func animate() {
NSLog("animate \(animationCounter)")
animateOpenVertical()
}
func dismiss() {
}
private func animateOpenVertical() {
animationCounter += 1
NSLog("animateOpenVertical \(animationCounter)")
UIView.animate(withDuration: self.animationTime, animations: {
var f = self.line1.frame
f.size.height = self.lineMax
f.origin.y = self.midY - ((self.lineMax)/2)
self.line1.frame = f
}) { (done) in
UIView.animate(withDuration: self.animationTime, animations: {
var f = self.line2.frame
f.size.height = self.lineMax
f.origin.y = self.midY - ((self.lineMax)/2)
self.line2.frame = f
}) { (done) in
UIView.animate(withDuration: self.animationTime, animations: {
var f = self.line3.frame
f.size.height = self.lineMax
f.origin.y = self.midY - ((self.lineMax)/2)
self.line3.frame = f
}) { (done) in
self.animateClosedVertical()
}
}
}
}
private func animateClosedVertical() {
animationCounter += 1
NSLog("animateClosedVertical \(animationCounter)")
UIView.animate(withDuration: self.animationTime, animations: {
var f = self.line1.frame
f.size.height = self.lineHeight
f.origin.y = self.midY - (self.lineHeight/2)
self.line1.frame = f
}) { (done) in
UIView.animate(withDuration: self.animationTime, animations: {
var f = self.line2.frame
f.size.height = self.lineHeight
f.origin.y = self.midY - (self.lineHeight/2)
self.line2.frame = f
}) { (done) in
UIView.animate(withDuration: self.animationTime, animations: {
var f = self.line3.frame
f.size.height = self.lineHeight
f.origin.y = self.midY - (self.lineHeight/2)
self.line3.frame = f
}) { (done) in
if self.animationCounter == 2 {
self.animateVertical()
}else {
self.animateVerticalReverse()
}
}
}
}
}
private func animateVertical() {
animationCounter += 1
NSLog("animateVertical \(animationCounter)")
UIView.animate(withDuration: self.animationTime, animations: {
var f1 = self.line1.frame
var f3 = self.line3.frame
f1.origin.x = CGFloat(self.midX - (self.lineWidth / 2))
f3.origin.x = CGFloat(self.midX - (self.lineWidth / 2))
f1.origin.y = self.midY - ((self.lineHeight / 2) + self.lineWidth + self.xMargin)
f3.origin.y = self.midY + ((self.lineHeight / 2) + self.xMargin)
self.line1.frame = f1
self.line3.frame = f3
}) { (done) in
self.animateOpenHorizontal()
}
}
private func animateOpenHorizontal() {
animationCounter += 1
NSLog("animateOpenHorizontal \(animationCounter)")
UIView.animate(withDuration: self.animationTime, animations: {
var f = self.line1.frame
f.size.width = self.lineMax
f.origin.x = self.midX - ((self.lineMax)/2)
self.line1.frame = f
}) { (done) in
UIView.animate(withDuration: self.animationTime, animations: {
var f = self.line2.frame
f.size.width = self.lineMax
f.origin.x = self.midX - ((self.lineMax)/2)
self.line2.frame = f
}) { (done) in
UIView.animate(withDuration: self.animationTime, animations: {
var f = self.line3.frame
f.size.width = self.lineMax
f.origin.x = self.midX - ((self.lineMax)/2)
self.line3.frame = f
}) { (done) in
self.animateClosedHorizontal()
}
}
}
}
private func animateClosedHorizontal() {
animationCounter += 1
NSLog("animateClosedHorizontal \(animationCounter)")
UIView.animate(withDuration: self.animationTime, animations: {
var f = self.line1.frame
f.size.width = self.lineWidth
f.origin.x = self.midX - (self.lineWidth/2)
self.line1.frame = f
}) { (done) in
UIView.animate(withDuration: self.animationTime, animations: {
var f = self.line2.frame
f.size.width = self.lineWidth
f.origin.x = self.midX - (self.lineWidth/2)
self.line2.frame = f
}) { (done) in
UIView.animate(withDuration: self.animationTime, animations: {
var f = self.line3.frame
f.size.width = self.lineWidth
f.origin.x = self.midX - (self.lineWidth/2)
self.line3.frame = f
}) { (done) in
if self.animationCounter == 5 {
self.animateHorizontalReverse()
}else {
self.animateHorizontal()
}
}
}
}
}
private func animateHorizontalReverse() {
animationCounter += 1
NSLog("animateHorizontalReverse \(animationCounter)")
UIView.animate(withDuration: self.animationTime, animations: {
var f1 = self.line1.frame
var f3 = self.line3.frame
f1.origin.x = self.midX + ((self.lineWidth / 2) + self.xMargin)
f3.origin.x = self.midX - ((self.lineWidth / 2) + self.lineWidth + self.xMargin)
f1.origin.y = self.midY - (self.lineHeight/2)
f3.origin.y = self.midY - (self.lineHeight/2)
self.line1.frame = f1
self.line3.frame = f3
}) { (done) in
self.animateOpenVertical()
}
}
private func animateVerticalReverse() {
animationCounter += 1
NSLog("animateVerticalReverse \(animationCounter)")
UIView.animate(withDuration: self.animationTime, animations: {
var f1 = self.line1.frame
var f3 = self.line3.frame
f1.origin.x = CGFloat(self.midX - (self.lineWidth / 2))
f3.origin.x = CGFloat(self.midX - (self.lineWidth / 2))
f1.origin.y = self.midY + ((self.lineHeight / 2) + self.xMargin)
f3.origin.y = self.midY - ((self.lineHeight / 2) + self.lineWidth + self.xMargin)
self.line1.frame = f1
self.line3.frame = f3
}) { (done) in
self.animateOpenHorizontal()
}
}
private func animateHorizontal() {
animationCounter += 1
NSLog("animateHorizontal \(animationCounter)")
UIView.animate(withDuration: self.animationTime, animations: {
var f1 = self.line1.frame
var f3 = self.line3.frame
f1.origin.x = self.midX - ((self.lineWidth / 2) + self.lineWidth + self.xMargin)
f3.origin.x = self.midX + ((self.lineWidth / 2) + self.xMargin)
f1.origin.y = self.midY - (self.lineHeight/2)
f3.origin.y = self.midY - (self.lineHeight/2)
self.line1.frame = f1
self.line3.frame = f3
}) { (done) in
self.animationCounter = 0
self.animateOpenVertical()
}
}
}
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
let spinner = JHSpinner(frame: view.frame)
view.backgroundColor = .white
view.addSubview(spinner)
spinner.animate()
PlaygroundPage.current.liveView = view
PlaygroundPage.current.needsIndefiniteExecution = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment