class ViewController: UIViewController {
var label : UILabel!
var textField:UITextField!
var result = arc4random_uniform(100)
//
override func viewDidLoad() {
super.viewDidLoad()
label = UILabel()
label.frame = CGRect(x: 80, y: 80, width: 100, height: 40)
label.text = "猜一猜"
label.backgroundColor = UIColor.redColor()
label.textColor = UIColor.greenColor()
label.font = UIFont.systemFontOfSize(28)
label.textAlignment = .Center
self.view.addSubview(label)
let label1 = UILabel()
label1.frame = CGRect(x: 50, y: 150, width: 40, height: 40)
label1.text = "0"
label1.backgroundColor = UIColor.redColor()
label1.textColor = UIColor.greenColor()
label1.font = UIFont.systemFontOfSize(28)
label1.textAlignment = .Center
self.view.addSubview(label1)
let label2 = UILabel()
label2.frame = CGRect(x: 100, y: 150, width: 40, height: 40)
label2.text = "到"
label2.backgroundColor = UIColor.redColor()
label2.textColor = UIColor.greenColor()
label2.font = UIFont.systemFontOfSize(28)
label2.textAlignment = .Center
self.view.addSubview(label2)
let label3 = UILabel()
label3.frame = CGRect(x: 150, y: 150, width: 60, height: 40)
label3.text = "100"
label3.backgroundColor = UIColor.redColor()
label3.textColor = UIColor.greenColor()
label3.font = UIFont.systemFontOfSize(28)
label3.textAlignment = .Center
self.view.addSubview(label3)
textField = UITextField()
textField.frame = CGRect(x: 100, y: 200, width: 100, height: 40)
textField.textColor = UIColor.blueColor()
textField.borderStyle = .RoundedRect
textField.placeholder = "请输入数字"
textField.keyboardType = .NumberPad
self.view.addSubview(textField)
let btn = UIButton(type: .System)
// btn.setTitle("确定", forState: .Normal)
// btn.setTitle("高亮", forState: .Highlighted)
// btn.setTitle("选用", forState: .Selected)
// btn.setTitle("禁用", forState: .Disabled)
//
let image = UIImage(named: "check")
btn.setBackgroundImage(image, forState: .Normal)
btn.frame = CGRect(x: 100, y: 300, width: 100, height: 100)
btn.addTarget(self, action: #selector(didClick), forControlEvents:.TouchUpInside)
self.view.addSubview(btn)
// Do any additional setup after loading the view, typically from a nib.
}
func didClick(){
let number = UInt32(textField.text!)
if result == number {
label.text = "猜对了"
result = arc4random_uniform(100)
}else if result < number {
label.text = "猜大了"
}else{
label.text = "猜小了"
}
}
网友评论