Skip to content

Instantly share code, notes, and snippets.

@benashman
Last active March 14, 2017 17:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benashman/2026c5e499f2c437e209 to your computer and use it in GitHub Desktop.
Save benashman/2026c5e499f2c437e209 to your computer and use it in GitHub Desktop.
Swift prototype helpers (WIP)
// Generate a random color
func randomColor() -> UIColor {
let hue : CGFloat = CGFloat(arc4random() % 256) / 256 // use 256 to get full range from 0.0 to 1.0
let saturation : CGFloat = CGFloat(arc4random() % 128) / 256 + 0.5 // from 0.5 to 1.0 to stay away from white
let brightness : CGFloat = CGFloat(arc4random() % 128) / 256 + 0.5 // from 0.5 to 1.0 to stay away from black
return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1)
}
// Execute block after delay
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
// Show network activity indicator icon
func showNetworkActivityIndicator() {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
// Hide network activity indicator icon
func hideNetworkActivityIndicator() {
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
// White status bar
// Make sure you've set `View controller-based status bar appearance = NO` in your .plist
func setLightStatusBar() {
UIApplication.sharedApplication().statusBarStyle = .LightContent
}
// Translate a value between two ranges
func modulate(value: CGFloat, r1Min: CGFloat, r1Max: CGFloat, r2Min: CGFloat, r2Max: CGFloat) -> CGFloat {
var ratio = (r2Max - r2Min) / (r1Max - r1Min)
return value * ratio + r2Min - r1Min * ratio
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment