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) 一篇不错的中文简单介绍 [译] ...
随机推荐
- Windows Redis安装,Java操作Redis
一.Redis 的安装 1.Redis 下载 Windows 版本下载:https://github.com/dmajkic/redis/downloads 2.解压到 C:\redis-2.4.5- ...
- java单例模式等一些程序的写法....持续更新...
一.单例模式的写法: public class MyFactory { /** * 饿汉式 */ private static MyFactory instance = new MyFactory() ...
- python 面向对象 初始化
参考学习: http://www.runoob.com/python/python-object.html 其中 函数里面 self.name 就是用 初始化的 name Employe.empCou ...
- arm-linux-gcc配置安装
1.首先去下载arm-linux-gcc压缩包 https://pan.baidu.com/s/15QLL-mf0G5cEJbVP5OgZcA 密码:ygf3 2.把压缩包导入到Linux系统:htt ...
- 如何制作简单的 3D 打印模型
Hi 大家好! 了解一个方兴未艾,但极为有趣的话题 — 3D 打印 . 为了帮助大家对3D打印有一个初步的感性认识,我在线制作了一款可用于3D打印的model, 大家可以先通过体验这个在线 model ...
- PHP生产二维码
1.引入phpqrcode包 <?php include 'phpqrcode.php'; QRcode::png('http://www.learnphp.cn',"code.png ...
- uva-10879-因数分解
把一个数分解成n*m的形式,一定存在 解题思路: 一个大于1的正整数最小因数一定是素数 ac时间80ms,感觉慢了,可惜看不到0ms的大神代码 #include <iostream> #i ...
- 页面白屏并且报错PHP Parse error: syntax error, unexpected end of file in 试了很久总算解决了
页面白屏并且报错PHP Parse error: syntax error, unexpected end of file in 试了很久 啥短标记,打开,都试了 最简单的办法 是重新建立一个文件, ...
- jsp 学习 第1步 - 引入 jstl
通过 eclipse 新建 动态web项目 默认是没有引入 jstl, 则无法JSP页面引入相关标记. <%@ taglib prefix="c" uri="ht ...
- 小程序-Now you can provide attr "wx:key" for a "wx:for" to improve performance
转自:https://www.cnblogs.com/xpwi/p/9878871.html 小程序开发-Now you can provide attr "wx:key" for ...