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.
}
}
最新文章
- Apache2 同源策略解决方案 - 配置 CORS
- POJ 1008 Maya Calendar
- css 使用background背景实现border边框效果
- .NET framework 4.0安装失败怎么办
- UVa11526 H(n)
- 简单的web三层架构系统【第二版】
- C#中is运算符
- ApplicaitionContext妙用request解耦合
- 用命令行打开sublime
- LOJ #2802. 「CCC 2018」平衡树(整除分块 + dp)
- HDU.4757.Tree(可持久化Trie)
- 记录css的常用属性
- bzoj1055玩具取名
- TPS和QPS定义以及影响TPS的因素
- 【Html基础】之<;h1>;~<;h6>; <;p>; <;br>; <;hr>;
- 基于testng自动化添加allure报告展示以及jenkins集成
- NodeJs中require use get typescript及其他知识点集合
- OpenNIDataGet 获取点云数据
- MySQL三方面优化
- chkconfig命令详细介绍