swift学习之UIButton
//
// 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的更多相关文章
- Swift学习之常用UI的使用
Swift学习之常用UI的使用 最近笔者在开始学习苹果最新的编程语言,因为笔者认为,苹果既然出了这门语言就绝对不会放弃,除非苹果倒闭了(当然这里知识一个玩笑). 所以在不久的将来,swift绝对是iO ...
- swift学习 - 计时器
swift学习之计时器 这个demo主要学习在swift中如何操作计时器(Timer),按钮(UIButton),文本(Label) 效果图: 代码 import UIKit class ViewCo ...
- 【swift学习笔记】二.页面转跳数据回传
上一篇我们介绍了页面转跳:[swift学习笔记]一.页面转跳的条件判断和传值 这一篇说一下如何把数据回传回父页面,如下图所示,这个例子很简单,只是把传过去的数据加上了"回传"两个字 ...
- IOS基础学习-2: UIButton
IOS基础学习-2: UIButton UIButton是一个标准的UIControl控件,UIKit提供了一组控件:UISwitch开关.UIButton按钮.UISegmentedContro ...
- 今天开始Swift学习
今天开始Swift学习 在此记录笔记 以备之后查阅! allenhuang
- iOS ---Swift学习与复习
swift中文网 http://www.swiftv.cn http://swifter.tips/ http://objccn.io/ http://www.swiftmi.com/code4swi ...
- 12套swift学习资源分享
虽然objective-c编程语言在过去很长一段时间都是iOS应用开发的基础语言,且很多iOS开发者对其也深爱有佳,但是随着swift编程语言的问世,迅速发展为开发者追捧的语言.且今年伴随着swift ...
- [转]swift 学习资源 大集合
今天看到了一个swift的学习网站,里面收集了很多学习资源 [转自http://blog.csdn.net/sqc3375177/article/details/29206779] Swift 介绍 ...
- swift 学习资源 大集合
今天看到一个swift学习网站,其中我们收集了大量的学习资源 Swift 介绍 Swift 介绍 来自 Apple 官方 Swift 简单介绍 (@peng_gong) 一篇不错的中文简单介绍 [译] ...
随机推荐
- linux之 LVM扩容
1. 查看本机现在磁盘的情况[root@oralce10g ~]# df Filesystem 1K-blocks Used Available Use% Mounted on/dev/mapper/ ...
- 深入理解java虚拟机,类加载
1,通过使用静态字段,只有真正定义这个字段的类才会被初始化,(子类不初始化,父类初始化 2,new数组,不初始化 3,通过类来调用一些类的常量,可以不初始化类,(常量在编译期,被优化到NotIniti ...
- CentOS7 RPM安装 rabbitmqDownloads on Bintray
下载 0依赖Erlang RPM for RabbitMQ包(https://github.com/rabbitmq/erlang-rpm) https://dl.bintray.com/rabbit ...
- springboot利用MockMvc测试controller控制器
主要记录一下控制器的测试,service这些类测试相对简单些(可测试性强) API测试需求比较简单: ① 需要返回正确的http状态码 200 ② 需要返回json数据,并且不能返回未经捕获的系统异常 ...
- 黄聪:VS2010中如何让webbrowser不弹出JS异常错误窗口(c#.net)
1.在属性窗口找到ScriptErrorsSuppressed,选择"true",这个选择的意思是,如果网页上有出现错误命令,这个错误提示将被抑制 2.[项目管理那里,在项目上右击 ...
- WinForm各浏览器内核控件
WebKit.NET webkit-sharp CefSharp awesomium OpenWebKitSharp geckofx MozNet Web Component
- PackedSyncPtr
folly/PackedSyncPtr.h A highly specialized data structure consisting of a pointer, a 1-bit spin lock ...
- HttpContext.Current并非无处不在
阅读目录 开始 无处不在的HttpContext HttpContext.Current到底保存在哪里? HttpContext并非无处不在! 如何获取文件绝对路径? 异步调用中如何访问HttpCon ...
- buffer cache —— buffer busy waits/read by other session
oracle提供非常精确.有效的row level lock机制,多个用户同时修改数据时,为了保护数据,以块为单位挂起锁的情况不会发生.但这不太正确.以块为单位的锁虽然不存在,但正因为oracle I ...
- Ubuntu上装KVM:安装、初次使用
KVM 是 Linux 内核自带的虚拟机系统, 使用它,你的机器就可以变成几台机了 试用过程如下: 环境: Ubuntu 14.04 64bit 1, KVM需要CPU硬件支持虚拟化,所以首先要确认 ...