IOS基础控件 UIButton swift3.1

//
//  ViewController.swift
//  UIButton
//
//  Created by hong wang on 2017/6/1.
//  Copyright © 2017年 appio. All rights reserved.
//


import UIKit


class ViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        //按钮Demo1
        let btn1:UIButton = UIButton(type: UIButtonType.system)
        //设置框架
        btn1.frame = CGRect(x: 10, y: 40, width: 50, height: 20)
        //按钮标题
        btn1.setTitle("按钮1", for: UIControlState.normal)
        //添加按钮到当前视图
        self.view.addSubview(btn1)
        //添加响应
        //btn1.addTarget(<#T##target: Any?##Any?#>, action: <#T##Selector#>, for: <#T##UIControlEvents#>)
        btn1.addTarget(self, action: #selector(ViewController.btn1Click), for: UIControlEvents.touchUpInside)
        
        //按钮Demo2
        let btn2:UIButton = UIButton(type: UIButtonType.system)
        btn2.frame = CGRect(x: 10, y: 70, width: 50, height: 20)
        btn2.setTitle("按钮2", for: UIControlState.normal)
        self.view.addSubview(btn2)
        btn2.addTarget(self, action: #selector(ViewController.btn2Click), for: UIControlEvents.touchUpInside)
        
        //按钮Demo3
        let btn3:UIButton = UIButton(type: UIButtonType.system )
        btn3.frame = CGRect(x: 10, y: 100, width: 50, height: 20)
        btn3.setTitle("按钮3", for: UIControlState.normal)
        btn3.setTitleColor(UIColor.white, for: UIControlState.normal )
        //btn3.layer.borderColor = UIColor.blue.cgColor;
        btn3.layer.backgroundColor = UIColor.blue.cgColor;
        //btn3.layer.borderWidth = 1;
        btn3.layer.cornerRadius = 3;
        self.view.addSubview(btn3)
        btn3.addTarget(self, action: #selector(ViewController.btn3Click), for: UIControlEvents.touchUpInside)
        
        //按钮Demo4
        let btn4:UIButton = UIButton(type: .system)
        btn4.frame = CGRect(x: 10, y: 130, width: 50, height: 20)
        btn4.setBackgroundImage(UIImage(named: "download.png"), for: .normal )
        self.view.addSubview(btn4)
        btn4.addTarget(self, action: #selector(ViewController.btn4Click), for: .touchUpInside)
        
        //按钮Demo5 type类型必须是custom不能是system,否则无法显示
        let btn5:UIButton = UIButton(type: .custom)
        btn5.frame = CGRect(x: 10, y: 160, width: 70, height: 30)
        btn5.setTitle("Btn5", for: .normal)
        btn5.setImage(UIImage(named: "s.png"), for: .normal)
        btn5.backgroundColor = UIColor.red
        self.view.addSubview(btn5)
        btn5.addTarget(self, action: #selector(ViewController.btn5Click), for: .touchUpInside)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


/*
     1. 随机函数 arc4random 返回类型为整形, 从0开始, 如果需要1-X之间的随机数: arc4random() % X + 1
     2. 随机生成指定范围内整形 arc4random_uniform( X ) 等同于 arc4random() % X
*/
    func btn1Click(){
            let iKey = arc4random() % 2
            print( iKey, "Btn1 Clicked!")
    }
    //带对象的函数响应
    func btn2Click(btn:UIButton){
        btn.backgroundColor = UIColor(colorLiteralRed: (Float)(arc4random() % 256) / 256, green: (Float)(arc4random() % 256 ) / 256, blue: (Float)(arc4random() % 256 ) / 256, alpha: 1)
    }
    
    func btn3Click(){
        print( "Btn3 Clicked!")
    }
    
    func btn4Click(){
        print( "Btn4 Clicked!")
    }
    
    func btn5Click(){
        print( "Btn5 Clicked!")
    }

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值