//

//  ViewController.swift

//  button

//

//  Created by su on 15/12/7.

//  Copyright © 2015年 tian. All rights reserved.

//

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

//创建一个button

let button:UIButton = UIButton(frame: CGRect(x: 110, y: 70, width: 100, height: 50))

button.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)

//设置不同状态的文字,依次是普通状态和按下状态

button.setTitle("你点我呀!", forState: UIControlState.Normal)

button.setTitle("你还真点啊", forState: UIControlState.Highlighted)

//通过addTarget方法为按钮添加交互响应,依次为按下事件

button.addTarget(self, action: "press:", forControlEvents: UIControlEvents.TouchUpInside)

self.view.addSubview(button)

//创建图形按钮

let normalImage = UIImage(named: "btn1")

let highLightedImage = UIImage(named: "btn2")

let button2 = UIButton(frame: CGRect(x: 110, y: 200, width: 100, height: 30))

button2.setImage(normalImage, forState: UIControlState.Normal)

button2.setImage(highLightedImage, forState: UIControlState.Highlighted)

self.view.addSubview(button2)

//创建一个图片加文字的按钮

let button3 = UIButton(frame: CGRect(x: 0, y: 250, width: 400, height: 30))

button3.setImage(UIImage(named: "btn1"), forState:UIControlState.Normal)

button3.titleLabel?.font = UIFont.systemFontOfSize(14)

button3.imageView?.contentMode = UIViewContentMode.ScaleAspectFit

button3.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

button3.setTitle("fsdfdsfsdfsadf", forState: UIControlState.Normal)

self.view.addSubview(button3)

//从系统定义的按钮类型常见button

let btn4:UIButton = UIButton(type: UIButtonType.Custom)

btn4.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

btn4.setTitle("12344444", forState: UIControlState.Normal)

btn4.titleLabel!.font = UIFont.systemFontOfSize(14)

btn4.frame = CGRect(x: 110, y: 300, width: 100, height: 100)

self.view.addSubview(btn4)

//创建部分圆角的按钮

let btn5:UIButton = UIButton(type: UIButtonType.Custom)

btn5.backgroundColor = UIColor.redColor()

btn5.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

btn5.setTitle("345555", forState: UIControlState.Normal)

btn5.titleLabel!.font = UIFont.systemFontOfSize(14)

btn5.frame = CGRect(x: 110, y: 400, width: 100, height: 100)

self.view.addSubview(btn5)

//        UIRectCornerTopLeft     = 1 << 0,

//        UIRectCornerTopRight    = 1 << 1,

//        UIRectCornerBottomLeft  = 1 << 2,

//        UIRectCornerBottomRight = 1 << 3,

//        UIRectCornerAllCorners  = ~0UL

//        let beizer:UIBezierPath = UIBezierPath(roundedRect: btn5.bounds, byRoundingCorners: UIRectCorner.TopLeft|UIRectCorner.TopRight, cornerRadii: CGSize(width: 15, height: 15))

let beizer:UIBezierPath = UIBezierPath(roundedRect: btn5.bounds, byRoundingCorners: [UIRectCorner.TopLeft,UIRectCorner.TopRight], cornerRadii: CGSize(width: 15, height: 15))

let shape:CAShapeLayer = CAShapeLayer()

shape.path = beizer.CGPath

btn5.layer.mask = shape

//创建border按钮

let btn9:UIButton = UIButton(frame: CGRect(x: 50, y: 500, width: 100, height: 35))

btn9.backgroundColor = UIColor.whiteColor()

btn9.setTitle("边框按钮", forState: UIControlState.Normal)

btn9.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

btn9.layer.borderColor = UIColor.blackColor().CGColor

btn9.layer.borderWidth = 1

btn9.layer.cornerRadius = 5

self.view.addSubview(btn9)

}

func press(sender:UIButton){

print("你按下了按钮")

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

swift学习之UIButton的更多相关文章

  1. Swift学习之常用UI的使用

    Swift学习之常用UI的使用 最近笔者在开始学习苹果最新的编程语言,因为笔者认为,苹果既然出了这门语言就绝对不会放弃,除非苹果倒闭了(当然这里知识一个玩笑). 所以在不久的将来,swift绝对是iO ...

  2. swift学习 - 计时器

    swift学习之计时器 这个demo主要学习在swift中如何操作计时器(Timer),按钮(UIButton),文本(Label) 效果图: 代码 import UIKit class ViewCo ...

  3. 【swift学习笔记】二.页面转跳数据回传

    上一篇我们介绍了页面转跳:[swift学习笔记]一.页面转跳的条件判断和传值 这一篇说一下如何把数据回传回父页面,如下图所示,这个例子很简单,只是把传过去的数据加上了"回传"两个字 ...

  4. IOS基础学习-2: UIButton

    IOS基础学习-2: UIButton   UIButton是一个标准的UIControl控件,UIKit提供了一组控件:UISwitch开关.UIButton按钮.UISegmentedContro ...

  5. 今天开始Swift学习

    今天开始Swift学习  在此记录笔记  以备之后查阅! allenhuang

  6. iOS ---Swift学习与复习

    swift中文网 http://www.swiftv.cn http://swifter.tips/ http://objccn.io/ http://www.swiftmi.com/code4swi ...

  7. 12套swift学习资源分享

    虽然objective-c编程语言在过去很长一段时间都是iOS应用开发的基础语言,且很多iOS开发者对其也深爱有佳,但是随着swift编程语言的问世,迅速发展为开发者追捧的语言.且今年伴随着swift ...

  8. [转]swift 学习资源 大集合

    今天看到了一个swift的学习网站,里面收集了很多学习资源 [转自http://blog.csdn.net/sqc3375177/article/details/29206779] Swift 介绍 ...

  9. swift 学习资源 大集合

    今天看到一个swift学习网站,其中我们收集了大量的学习资源 Swift 介绍 Swift 介绍 来自 Apple 官方 Swift 简单介绍 (@peng_gong) 一篇不错的中文简单介绍 [译] ...

随机推荐

  1. Sql Server中执行计划的缓存机制

    Sql查询过程 当执行一个Sql语句或者存储过程时, Sql Server的大致过程是 1. 对查询语句进行分析,将其生成逻辑单元,并进行基本的语法检查 2. 生成查询树(会将查询语句中所有操作转换为 ...

  2. CentOS7 防火墙配置-详解

    CentOS 7 防火墙配置 1.防火墙的简述 防火墙对服务器起到一定的保护作用,所以了解一些相关的操作是很有必要的. 在CentOS 7.x中,有了一种新的防火墙策略叫FireWall , 在6.x ...

  3. oracle undo表空间大小修改

    redhat:清空回收站 rm -rf  /home/登录用户名/.Trash 例子:rm -rf /home/.Trash-root df命令可以显示目前所有文件系统的可用空间及使用情形: 例子:d ...

  4. Python download a image (or a file)

    http://stackoverflow.com/questions/13137817/how-to-download-image-using-requests import shutil impor ...

  5. create index 与 alter table add index 区别

    众所周知,MySQL创建索引有两种语法,即:ALTER TABLE HeadOfState ADD INDEX (LastName, FirstName);CREATE INDEX index_nam ...

  6. poj 1930 Dead Fraction(循环小数化分数)

    Dead Fraction Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3478   Accepted: 1162 Des ...

  7. 1027代码审计平台 3 Java maven

    使用成熟的构建工具对maven工程进行分析 官网:SCAN/Analyzing+with+SonarQube+Scanner+for+Maven 1.如下图修改settings.xml文件 1.1查看 ...

  8. Custom Exception in ASP.NET Web API 2 with Custom HttpResponse Message

    A benefit of using ASP.NET Web API is that it can be consumed by any client with the capability of m ...

  9. 转 linux 下装 usb driver

    http://www.george-smart.co.uk/wiki/Xilinx_JTAG_Linux

  10. 传统项目利用Hystrix实现热点接口的服务隔离

    这段时间接了个需求,需要在我目前负责的数据系统上加个接口,主要是实现用户行为的记录.前端对接的项目主要有公司的PC,WAP,WEIXIN,APP等,每个端大概有两台左右的负载.因为目前我的这个项目主要 ...