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.

}

}

时间: 2024-08-23 12:42:36

swift学习之UIButton的相关文章

Swift学习之常用UI的使用

Swift学习之常用UI的使用 最近笔者在开始学习苹果最新的编程语言,因为笔者认为,苹果既然出了这门语言就绝对不会放弃,除非苹果倒闭了(当然这里知识一个玩笑). 所以在不久的将来,swift绝对是iOS 开发的主导语言,也许不会完全取代OC. 笔者学完swift的语法之后就开始着手UI了,因为我觉得有着一定的OC基础.所以这里关于swift的语法就不做多介绍了,在后面的文章中,我将会详细介绍一下关于swift中的重点,难点语法和一些新特性. 下面是我在学习UI的时候自己总结的一些swift创建U

Swift 学习-多线程

1:第一种多线程 func fun1(){ for i in 200...300{ NSLog("%d",i); } } func fun2(){ for i in 300...400{ NSLog("%d",i); } } var th1 = NSThread(target:self,selector:"fun1",object:nil); th1.start(); //开启线程 NSThread.detachNewThreadSelector

SWIFT学习笔记05

1.Swift 无需写break,所以不会发生这种贯穿(fallthrough)的情况.2.//用不到变量名,可用"_"替换 for _ in 1...power { answer *= base } 3.case 可以匹配更多的类型模式,包括区间匹配(range matching),元组(tuple)和特定类型的描述. 可以这样用case case 1...3: naturalCount = "a few" 4.如果存在多个匹配,那么只会执行第一个被匹配到的 ca

SWIFT学习笔记02

1.//下面的这些浮点字面量都等于十进制的12.1875: let decimalDouble = 12.1875 let exponentDouble = 1.21875e1 let hexadecimalDouble = 0xC.3p0//==12+3*(1/16) 2.//类型别名,用typealias关键字来定义类型别名 typealias AudioSample = UInt16 var maxAmplitudeFound = AudioSample.min 3.//元组 let ht

swift学习笔记(三)关于拷贝和引用

在swift提供的基本数据类型中,包括Int ,Float,Double,String,Enumeration,Structure,Dictionary都属于值拷贝类型. 闭包和函数同属引用类型 捕获则为拷贝.捕获即定义这些常量和变量的原作用域已不存在,闭包仍然可以在闭包函数体内引用和修改这些值 class属于引用类型. Array的情况稍微复杂一些,下面主要对集合类型进行分析: 一.关于Dictionary:无论何时将一个字典实例赋给一个常量,或者传递给一个函数方法时,在赋值或调用发生时,都会

Swift学习笔记十二:下标脚本(subscript)

下标脚本就是对一个东西通过索引,快速取值的一种语法,例如数组的a[0].这就是一个下标脚本.通过索引0来快速取值.在Swift中,我们可以对类(Class).结构体(structure)和枚举(enumeration)中自己定义下标脚本的语法 一.常规定义 class Student{ var scores:Int[] = Array(count:5,repeatedValue:0) subscript(index:Int) -> Int{ get{ return scores[index];

Swift学习笔记四:数组和字典

最近一个月都在专心做unity3d的斗地主游戏,从早到晚,最后总算是搞出来了,其中的心酸只有自己知道.最近才有功夫闲下来,还是学习学习之前的老本行--asp.net,现在用.net做项目流行MVC,而不是之前的三层,既然技术在更新,只能不断学习,以适应新的技术潮流! 创建MVC工程 1.打开Visual studio2012,新建MVC4工程 2.选择工程属性,创建MVC工程 3.生成工程的目录 App_Start:启动文件的配置信息,包括很重要的RouteConfig路由注册信息 Conten

Swift学习——使用if和switch来进行条件操作,使用for,while,和do-while来进行循环(三)

Swift学习--使用if和switch来进行条件操作,使用for,while,和do-while来进行循环 //switch支持随意类型的数据以及各种比較操作--不不过整数以及測试相等 //注意假设去掉default程序会报错 let strings = "hello3" switch strings{ case "hello1": let stringsComment = "say hello1" println("stringsC

汇集了很多swift 学习指南

https://github.com/ipader/SwiftGuide 1,059   Unstar7,294 Fork1,966 ipader/SwiftGuide CodeIssues 0Pull requests 0WikiPulseGraphs 这份指南汇集了Swift语言主流学习资源,并以开发者的视角整理编排.http://dev.swiftguide.cn 376 commits 3 branches 0 releases 12 contributors Swift 100.0%