//

//  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. 如何利用 AVDictionary 配置参数(转)

    本文是我的 FFMPEG Tips 系列的第五篇文章,准备介绍下 ffmpeg 提供的一个非常好用的健值对工具:AVDictionary,特别是对于没有 map 容器的 c 代码,可以充分利用它来配置 ...

  2. windows2008 安装oracle10g“程序异常终止。发生内部错误。请将以下文件提供给oracle技术支持部门

    在安装oracle10g客户端程序的时候发生了错误!错误如下:“程序异常终止.发生内部错误.请将以下文件提供给oracle技术支持部门:“未知”“未知”“未知” 解决办法: 右键点setup.exe ...

  3. 重温MySQL

    登录 mysql -uroot -p123456 查询当前用户 select user(); 不执行某条语句 \c 查看帮助信息 help create user; 创建用户 create user ...

  4. debian修改ssh端口

    vim /etc/ssh/sshd_config,修改port为你想要的端口 invoke-rc.d ssh restart 客户端连接: ssh 142.4.125.90 -l root -p875 ...

  5. 智能家居入门DIY——【四、组合】

    前面几篇介绍了一些传感器和代码,这篇介绍一下把它们组合起来.之所以单独列出这部分,原因在于组合更多功能的时候发现使用软串口库驱动ESP8266时由于内存过小导致发送失败甚至整个系统无法工作的情况.所以 ...

  6. 黄聪:sqlserver 2008修改数据库表的时候错误提示“阻止保存要求重新创建表的更改”

    当用户在在SQL Server 2008企业管理器中更改表结构时,必须要先删除原来的表, 然后重新创建新表,才能完成表的更改,如果强行更改会出现以下提示:不允许保存更改 .您所做的更改要求删除并重新创 ...

  7. JVM体系结构之三:方法区之2(jdk1.6,jdk1.7,jdk1.8下的方法区变迁)

    方法区 方法区存储虚拟机加载的类信息,常量,静态变量,即时编译器编译后的代码等数据.HotSpot中也称为永久代(Permanent Generation),(存储的是除了Java应用程序创建的对象之 ...

  8. Windows 远程桌面连接Ubuntu16.04图像界面

    1.安装xrdp sudo apt-get install xrdp 2. 安装vnc4server sudo apt-get install vnc4server 3. 安装xubuntu-desk ...

  9. 基于git的管理应用程序基线包和版本

    由于工作的需要,身为git的小白的我开始研究git相关的命令和操作.结合网上收集和廖雪峰的git教程,记录所学知识点. 相关的效果就不再这里显示了. 首先我们看一下git的常用命令: 常用命令 git ...

  10. 将bmp文件转换为jpg文件

    procedure TForm1.Button1Click(Sender: TObject);(*压缩MBP为JPEG;但是没有提供压缩比可选项凑合用吧,大概1/3 ^_^:Note:必须加上JPEG ...