//
// ViewController.swift
// Demo
//
// Created by gang.zhou on 2018/9/22.
// Copyright © 2018年 gang.zhou. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
private var button:UIButton? = nil
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
view.backgroundColor = UIColor.red
button = UIButton.init(frame: CGRect.init(x: (view.frame.width * 0.1), y: (view.frame.height - 40)/2, width: (view.frame.width * 0.8), height: 40))
button!.setTitle("This is button,click me!", for: .normal)
button!.backgroundColor = UIColor.yellow
button!.setTitleColor(UIColor.red, for: .normal)
view.addSubview(button!)
button!.addTarget(self, action:(#selector(touchDown)), for: UIControl.Event.touchDown)
button!.addTarget(self, action:(#selector(touchUp)), for: UIControl.Event.touchUpInside)
button!.addTarget(self, action:(#selector(touchUp)), for: UIControl.Event.touchUpOutside)
}
@objc func touchDown(){
if let button = button {
button.backgroundColor = UIColor.cyan
}
}
@objc func touchUp(){
if let button = button {
button.backgroundColor = UIColor.yellow
}
}
}
网友评论