import UIKit
class ViewController: UIViewController {
override func viewDidLoad() { super.viewDidLoad() let button = UIButton(type:.system); button.frame = CGRect.init(x:100,y:150,width:160,height:30); button.setTitle("changColor", for: .normal); button.backgroundColor = UIColor.brown; button.titleLabel?.font = UIFont.systemFont(ofSize: 17); button.tintColor = UIColor.cyan; button.addTarget(self, action: #selector(buttonDidClick), for: .touchUpInside); self.view.addSubview(button); let smallView = UIView(); smallView.frame = CGRect.init(x: 50, y: 50, width: 100, height: 100); smallView.backgroundColor = UIColor.darkGray; smallView.tag = 100; self.view.addSubview(smallView); } func buttonDidClick() { print("按钮被点击了"); let changView = self.view.viewWithTag(100); changView?.backgroundColor = UIColor.init(colorLiteralRed: Float(CGFloat(arc4random()%256)/255.0), green: Float(CGFloat(arc4random()%256)/255.0), blue: Float(CGFloat(arc4random()%256)/255.0), alpha: 1.0) }
} 详细代码查看:https://github.com/xiaolitou-ping/Swift-All
|
请发表评论