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 下 ifcfg-eth0 配置/CentOS_minimal安装和开发环境部署
CentOS_minimal安装和开发环境部署:http://www.th7.cn/system/lin/201305/39002.shtml 网络接口配置文件 [root@localhost ~]# ...
- PLSQL在64位 win7/WIN8上运行(绿色版plsql、无需安装oracle客户端)
一.准备需要的文件: 1.plsql文件绿色压缩包 2.oracle客户端文件绿色压缩包 二.设置PLSQL 运行plsql,不登陆进入plsql点“工具”-“首选项” 按照如图设置“ oracle主 ...
- coding github 配置ssl 免密拉取代码
详细介绍: https://www.cnblogs.com/superGG1990/p/6844952.html 注:其中检验过程与下述不同,可以先在对应git库使用 git pull 一次,选择信任 ...
- Python VIL Service Bin
#!/usr/bin/python #coding:UTF-8 import sys import re import getopt import md5 import os import subpr ...
- 【转】深入理解Java的接口和抽象类
深入理解Java的接口和抽象类 对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太多不同的地方.很多人在初学的 ...
- scrollWidth,clientWidth,offsetWidth的区别 ---转载的
转载自博客:http://www.cnblogs.com/kongxianghai/p/4192032.html 通过一个demo测试这三个属性的差别. 说明: scrollWidth:对象的实际内容 ...
- Jenkins迁移
Jenkins:http://192.168.1.240:8080/jenkins 重启jenkins: sudo /etc/init.d/jenkins start sudo /etc/init.d ...
- java高并发总结-常用于面试复习
定义: 独占锁是一种悲观保守的加锁策略,它避免了读/读冲突,如果某个只读线程获取锁,则其他读线程都只能等待,这种情况下就限制了不必要的并发性,因为读操作并不会影响数据的一致性. 共享锁则是一种乐观锁, ...
- 前端开发中常用的CSS选择器解析(一)
你也许已经掌握了id.class.后台选择器这些基本的css选择器.但这远远不是css的全部.下面向大家系统的介绍css中最常用的选择器,包括我们最头痛的浏览器兼容性问题.掌握了它们,才能真正领略cs ...
- VB.Net与C# 的语法比较
最近看代码或写代码时,经常把VB与C#的基本语法搞混,为方便查看,特对其异同进行对比: 變數初始化 VB.NET 自動將所有的變數初始化成 0 或 nothing.C# 在你未初始化變數之前不准你用該 ...