Created
October 31, 2019 05:58
-
-
Save nathan-fiscaletti/b1661a0f48df3220a7cfb97d37f71f91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController { | |
let gridLocationsLock = DispatchSemaphore(value: 1) | |
var activeGridLocations:[CGPoint] = [] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.view.backgroundColor = UIColor.black | |
let numOfActiveViews = 50 | |
for _ in 0..<numOfActiveViews { | |
spawnRandomView(ofSize: CGSize(width: 50.0, height: 50.0)) | |
} | |
} | |
func spawnRandomView(ofSize size: CGSize) { | |
gridLocationsLock.wait() | |
let elementWidth = Int(size.width) + 8 | |
let elementHeight = Int(size.height) + 8 | |
var grid:[CGPoint] = [] | |
let gridCountX:Int = Int(self.view.frame.width) / Int(elementWidth) | |
let gridCountY:Int = Int(self.view.frame.height) / Int(elementHeight) | |
for gx in 0..<gridCountX { | |
for gy in 0..<gridCountY { | |
let point = CGPoint(x: CGFloat((Int(elementWidth) * gx) + 16), y: CGFloat((Int(elementHeight) * gy) + 32)) | |
if !activeGridLocations.contains(where: { (p: CGPoint) -> Bool in | |
return round(p.x) == round(point.x) && round(p.y) == round(point.y) | |
}) { | |
grid.append(point) | |
} | |
} | |
} | |
let gridLocation = grid[Int.random(in: 0..<grid.count)] | |
activeGridLocations.append(gridLocation) | |
gridLocationsLock.signal() | |
spawnView(at: gridLocation, withSize: size) | |
} | |
func spawnView(at location: CGPoint, withSize size: CGSize) { | |
let view = UIView(frame: CGRect( | |
x: location.x, | |
y: location.y, | |
width: size.width, | |
height: size.height | |
)) | |
let randomColorHex = Int32(arc4random_uniform(16777216)) | |
let randomColor = UIColor.init(red: CGFloat((randomColorHex>>16)&0xFF) / 255.0, green: CGFloat((randomColorHex>>8)&0xFF) / 255.0, blue: CGFloat(randomColorHex&0xFF) / 255.0, alpha: 1.0) | |
view.backgroundColor = randomColor | |
view.alpha = 0 | |
view.layer.cornerRadius = CGFloat(Int.random(in: 0...23)) | |
view.clipsToBounds = true | |
self.view.addSubview(view) | |
UIView.animate(withDuration: 0.4, animations: { | |
view.alpha = 1.0 | |
}, completion: { (finished: Bool) in | |
if finished { | |
self.runIdleAnimation(onView: view, withDelay: TimeInterval(Float.random(in: 0.0...1.0)), maxTimes: Int.random(in: 1...3)) | |
} | |
}) | |
} | |
func runIdleAnimation(onView view: UIView, withDelay delay: TimeInterval = TimeInterval(0), runCount count: Int = 1, maxTimes: Int = 0) { | |
let oldPoint = view.frame.origin | |
UIView.animate(withDuration: 0.4, delay: delay, options: .curveLinear, animations: { | |
view.frame = CGRect( | |
x: view.frame.origin.x, | |
y: view.frame.origin.y - 10, | |
width: view.frame.size.width, | |
height: view.frame.size.height | |
) | |
}, completion: { (finished: Bool) in | |
if finished { | |
UIView.animate(withDuration: 0.4, animations: { | |
view.transform = view.transform.rotated(by: CGFloat(Double.pi)) | |
}, completion: { (finished2: Bool) in | |
if finished2 { | |
UIView.animate(withDuration: 0.4, delay: 0.0, options: .curveLinear, animations: { | |
view.frame = CGRect(x: oldPoint.x, y: oldPoint.y, width: view.frame.size.width, height: view.frame.size.height) | |
}, completion: { (finished3: Bool) in | |
if finished3 { | |
if maxTimes > 0 { | |
if count == maxTimes { | |
let explode = Bool.random() | |
if explode { | |
let originalPoint = view.frame.origin | |
let originalSize = view.frame.size | |
let implode = Bool.random() | |
if implode { | |
UIView.animate(withDuration: 0.4, animations: { | |
view.frame = CGRect(x:originalPoint.x, y:originalPoint.y, width: 0, height: 0) | |
view.alpha = 0.0 | |
}, completion: { (finished4: Bool) in | |
if finished4 { | |
self.gridLocationsLock.wait() | |
self.activeGridLocations.removeAll { (p: CGPoint) -> Bool in | |
return round(p.x) == round(originalPoint.x) && round(p.y) == round(originalPoint.y) | |
} | |
self.gridLocationsLock.signal() | |
view.removeFromSuperview() | |
self.spawnRandomView(ofSize: originalSize) | |
return | |
} | |
}) | |
} else { | |
UIView.animate(withDuration: 0.4, animations: { | |
view.frame = CGRect(x:0, y:0, width: self.view.frame.width, height: self.view.frame.height) | |
view.alpha = 0.0 | |
}, completion: { (finished4: Bool) in | |
if finished4 { | |
self.gridLocationsLock.wait() | |
self.activeGridLocations.removeAll { (p: CGPoint) -> Bool in | |
return round(p.x) == round(originalPoint.x) && round(p.y) == round(originalPoint.y) | |
} | |
self.gridLocationsLock.signal() | |
view.removeFromSuperview() | |
self.spawnRandomView(ofSize: originalSize) | |
return | |
} | |
}) | |
} | |
} else { | |
UIView.animate(withDuration: 0.4, animations: { | |
view.alpha = 0.0 | |
}, completion: { (finished4: Bool) in | |
if finished4 { | |
self.gridLocationsLock.wait() | |
self.activeGridLocations.removeAll { (p: CGPoint) -> Bool in | |
return round(p.x) == round(view.frame.origin.x) && round(p.y) == round(view.frame.origin.y) | |
} | |
self.gridLocationsLock.signal() | |
view.removeFromSuperview() | |
self.spawnRandomView(ofSize: view.frame.size) | |
return | |
} | |
}) | |
} | |
return | |
} | |
} | |
self.runIdleAnimation(onView: view, runCount: count + 1, maxTimes: maxTimes) | |
} | |
}) | |
} | |
}) | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment