Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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