• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

IOS基础控件 UIButton swift3.1

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
//
//  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!")
    }

}





鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
iOS开发Swift篇—(六)流程控制发布时间:2022-07-13
下一篇:
swift学习笔记3——类、结构体、枚举发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap