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) 一篇不错的中文简单介绍 [译] ...
随机推荐
- string类的简要实现
#include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #inc ...
- Ubuntu14.04下Sublime Text 3解决无法输入中文
在Ubuntu 14.04中安装了SublimeText 3之后发现既然不支持输入中文,于是在网上搜罗一下,发现很多人遇到了同样的问题,但是解决办法大该就只有一个.下面根据自身的安装及解决办法总结如下 ...
- 黄聪:360浏览器、chrome开发扩展插件教程(2)为html添加行为
转载:http://www.cnblogs.com/walkingp/archive/2011/04/02/2002668.html 上一节我们已经讲了Chrome扩展的基础知识,并构建了基础的htm ...
- Java 字符串 String
什么是Java中的字符串 在 Java 中,字符串被作为 String 类型的对象处理. String 类位于 java.lang 包中.默认情况下,该包被自动导入所有的程序. 创建 String 对 ...
- 熟练使用Linux系统信息类命令
系统信息类命令 – dmesg命令 dmesg命令用实例名和物理名称来标识连到系统上的设备. dmesg命令显示系统诊断信息.操作系统版本号.物理内存大小以及其他信息. 系统启动时,屏幕上会显示系统C ...
- logger5步走
https://www.cnblogs.com/GGGGGGZX/p/9114378.html'''打印日志11/26/2017 10:44:21 PM bug 24 并写入文件example.log ...
- 线程的等待方法:join
其实多线程最复杂的地方在于不同线程间的同步问题,这其中会涉及到先后执行问题.共享变量问题等.这篇文章我们主要来开个头,看一下join方法. using System; using System.Thr ...
- 2017 年 PHP 程序员未来路在何方?
PHP 从诞生到现在已经有20多年历史,从Web时代兴起到移动互联网退潮,互联网领域各种编程语言和技术层出不穷, Node.js . GO . Python 不断地在挑战 PHP 的地位.这些技术的推 ...
- 消息队列kafka
消息队列kafka 为什么用消息队列 举例 比如在一个企业里,技术老大接到boss的任务,技术老大把这个任务拆分成多个小任务,完成所有的小任务就算搞定整个任务了. 那么在执行这些小任务的时候,可能 ...
- 8.3.2018 1 Quick and dirty 快而脏的快餐
Quick and dirty 快而脏的快餐 BEIJING 北京 Food delivery is a booming business. Waste is piling up, too 送餐 ...