swift学习 - 计时器
swift学习之计时器
这个demo主要学习在swift中如何操作计时器(Timer),按钮(UIButton),文本(Label)
效果图:
代码
import UIKit
class ViewController: UIViewController {
var btn1:UIButton?
var timer:Timer?
var label:UILabel?
var a = 0.0
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
func setupUI() {
let button = UIButton(frame:CGRect(x: 20, y: 400, width:50, height: 50))
button.backgroundColor = .red
button.addTarget(self, action: #selector(startCLick(item:)), for: .touchUpInside)
button.layer.cornerRadius = 25
button.clipsToBounds = true
button.setTitle("开始", for: .normal)
btn1 = button
view.addSubview(button)
let btn = UIButton(frame:CGRect(x: 300, y: 400, width: 50, height: 50))
btn.backgroundColor = .blue
btn.addTarget(self, action: #selector(startCLick(item:)), for: .touchUpInside)
btn.setTitle("暂停", for: UIControlState.normal)
view .addSubview(btn)
let lab = UILabel(frame: CGRect(x: 50, y: 50, width: view.frame.size.width - 100, height: 50))
lab.textAlignment = .center
lab.font = .systemFont(ofSize: 18)
lab.backgroundColor = .red
lab.textColor = .white
view.addSubview(lab)
label = lab
lab.text = "swift stopWatch Demo"
}
func timerIntervalx() {
a+=1;
label?.text = "swift stopWatch Demo \(a)"
}
func startCLick(item:UIButton) {
if item.isEqual(btn1) {
timeStart()
}
else
{
timePause()
}
}
func timeStart() {
if !(timer != nil) {
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerIntervalx), userInfo: nil, repeats: true)
}
}
func timePause() {
timer?.invalidate()
timer = nil
}
}
基本控件的创建:
UIButton:
let btn = UIButton(frame:CGRect(x: 300, y: 400, width: 50, height: 50))
UILabel:
let lab = UILabel(frame: CGRect(x: 50, y: 50, width: view.frame.size.width - 100, height: 50))
Timer:
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerIntervalx), userInfo: nil, repeats: true)
swift中方法名更加简单,枚举类型可以直接使用点语法操作
.touchUpInside .white .center
swift学习 - 计时器的更多相关文章
- 【swift学习笔记】二.页面转跳数据回传
上一篇我们介绍了页面转跳:[swift学习笔记]一.页面转跳的条件判断和传值 这一篇说一下如何把数据回传回父页面,如下图所示,这个例子很简单,只是把传过去的数据加上了"回传"两个字 ...
- 今天开始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学习之常用UI的使用
Swift学习之常用UI的使用 最近笔者在开始学习苹果最新的编程语言,因为笔者认为,苹果既然出了这门语言就绝对不会放弃,除非苹果倒闭了(当然这里知识一个玩笑). 所以在不久的将来,swift绝对是iO ...
- [转]swift 学习资源 大集合
今天看到了一个swift的学习网站,里面收集了很多学习资源 [转自http://blog.csdn.net/sqc3375177/article/details/29206779] Swift 介绍 ...
- swift 学习资源 大集合
今天看到一个swift学习网站,其中我们收集了大量的学习资源 Swift 介绍 Swift 介绍 来自 Apple 官方 Swift 简单介绍 (@peng_gong) 一篇不错的中文简单介绍 [译] ...
- Swift学习笔记(一)搭配环境以及代码运行成功
原文:Swift学习笔记(一)搭配环境以及代码运行成功 1.Swift是啥? 百度去!度娘告诉你它是苹果最新推出的编程语言,比c,c++,objc要高效简单.能够开发ios,mac相关的app哦!是苹 ...
- swift学习:第一个swift程序
原文:swift学习:第一个swift程序 最近swift有点火,赶紧跟上学习.于是,个人第一个swift程序诞生了... 新建项目
随机推荐
- 老李分享:MySql的insert语句的性能优化方案
老李分享:MySql的insert语句的性能优化方案 性能优化一直是测试人员比较感兴趣的内容,poptest在培训学员的时候也加大了性能测试调优的方面的内容,而性能优化需要经验的积累,经验的积累依 ...
- Android 开发之错误整理java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10088 nor current process has android.permission.READ_PHONE_STATE.
java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10088 nor current process has a ...
- Java线程池使用和分析(二) - execute()原理
相关文章目录: Java线程池使用和分析(一) Java线程池使用和分析(二) - execute()原理 execute()是 java.util.concurrent.Executor接口中唯一的 ...
- Circular placeholder reference 'jdbc.driver' in property definitions
Caused by: java.lang.IllegalArgumentException: Circular placeholder reference 'jdbc.driver' in prope ...
- Java关于e.printStackTrace()介绍
public void printStackTrace()将此 throwable 及其追踪输出至标准错误流.此方法将此 Throwable 对象的堆栈跟踪输出至错误输出流,作为字段 System.e ...
- apache-maven-3.3.9 环境配置
本地 maven 环境 安装 Maven 之前要求先确定你的 JDK 已经安装配置完成.Maven是 Apache 下的一个项目,目前我的新版本是 3.3.9. 1.官网下载 Maven:http:/ ...
- Jmeter自动化测试工具的简单使用--HTTP测试
Jmeter自动化测试工具的简单应用 1.安装Jmeter 链接: https://pan.baidu.com/s/1mhSzU68 密码: 446z 到这里下载 1.1 jmeter 将下载好的 ...
- mybatis新手入门常见问题集(持续更新)
一.参数为集合 Q:parameterType指的的类型是集合类型还是对象? A:都可以,甚至不用在xml中指定也可以.第一,mybatis会对传入的参数进行判断是不是list或者array,第二,m ...
- 【小瑕疵】表单中的button会自动提交?
在表单中使用button标签的时候会发现,即使什么类型都没有设置,但是在点击的时候会发现,表单会自动提交 比如: 我在一个表单的button中添加一个函数,当点击这个按钮时会增加一行内容: 但是当我实 ...
- 一个 Vue + Node + MongoDB 博客系统
源码 耗时半载(半个月)的大项目终于完成了.这是一个博客系统,使用 Vue 做前端框架,Node + express 做后端,数据库使用的是 MongoDB.实现了用户注册.用户登录.博客管理(文章的 ...