Skip to content

Instantly share code, notes, and snippets.

@erica
Last active August 23, 2016 18:18
Show Gist options
  • Save erica/57dd27ea693b36b79db5581684e6268b to your computer and use it in GitHub Desktop.
Save erica/57dd27ea693b36b79db5581684e6268b to your computer and use it in GitHub Desktop.
import UIKit
import PlaygroundSupport
let vc = UIViewController()
vc.view.backgroundColor = .white
PlaygroundPage.current.liveView = vc
PlaygroundPage.current.needsIndefiniteExecution = true
let button = UIButton(type: .system)
button.frame = CGRect(x: 0, y: 0, width: 240, height: 120)
button.setTitle("Push Me", for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 32)
vc.view.addSubview(button)
button.center = vc.view.center
let label = UILabel(frame: CGRect(x: 0, y:0, width:200, height: 30))
label.center = CGPoint(x: button.center.x, y: button.center.y + 60)
label.text = "What will you win?"
label.textAlignment = .center
vc.view.addSubview(label)
extension UIViewController {
func go() {
let roll = arc4random_uniform(100)
switch roll {
case 0 ..< 60: label.text = "You won a penny"
case 60 ..< 80: label.text = "You won two pennies"
case 80 ..< 90: label.text = "You won a nickel"
case 90 ..< 97: label.text = "You won a dime"
default: label.text = "You won a quarter"
}
}
}
button.addTarget(vc, action: #selector(UIViewController.go), for: .touchUpInside)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment