swfit-计时器
2024-10-18 18:13:12
import UIKit class FourVC: UIViewController { var label:UILabel = UILabel()
var index : Int =
var timer:Timer = Timer()
override func viewDidLoad() {
super.viewDidLoad() label = UILabel.init()
label.frame = CGRect.init(x:,y:,width: ,height:)
label.text = "计时器"
label.textColor = UIColor.black
self.view.addSubview(label) timer = Timer.scheduledTimer(timeInterval: , target: self, selector: #selector(createTimer), userInfo: nil, repeats: true)
RunLoop.current.add(timer, forMode: RunLoopMode.commonModes)
timer.fireDate = NSDate.distantFuture let endTimeBtn : UIButton = UIButton()
endTimeBtn.frame = CGRect.init(x:,y:,width: ,height:)
endTimeBtn.backgroundColor = UIColor.cyan
endTimeBtn.setTitle("停止计时", for: UIControlState.normal)
endTimeBtn.titleColor(for: UIControlState.normal)
endTimeBtn.setTitleColor(UIColor.black, for: UIControlState.normal)
endTimeBtn.addTarget(self, action: #selector(pressBtn1), for: UIControlEvents.touchUpInside)
self.view.addSubview(endTimeBtn) let startTimeBtn : UIButton = UIButton()
startTimeBtn.frame = CGRect.init(x:,y:,width: ,height:)
startTimeBtn.backgroundColor = UIColor.cyan
startTimeBtn.setTitle("开始计时", for: UIControlState.normal)
startTimeBtn.titleColor(for: UIControlState.normal)
startTimeBtn.setTitleColor(UIColor.black, for: UIControlState.normal)
startTimeBtn.addTarget(self, action: #selector(pressBtn), for: UIControlEvents.touchUpInside)
self.view.addSubview(startTimeBtn) } func pressBtn(){ //开始计时,很远的过去
timer.fireDate = NSDate.distantPast } func pressBtn1(){
//关闭计时,很远的将来
timer.fireDate = NSDate.distantFuture }
func createTimer(){ index +=
label.text = "\(index)" if index == {
//关闭计时器
timer.fireDate = NSDate.distantFuture
label.text = "倒计时停止\(index)"
} } override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
最新文章
- jquery css事件编程 位置 操作
- 模拟apache commons dbutils 实现自己的BeanListHandler(回调应用)
- PHP内核探索之变量(3)- hash table
- HTML 透明、阴影,圆角等知识点
- js方法在iframe父子窗口
- CF781D Axel and Marston in Bitland [倍增 矩阵乘法 bitset]
- CSS3总结(干货)
- MTK 使用iptable 命令来完成网络路由(android WIFI/4G分享网络)
- AX_xSession
- VC 中引用js文件
- 【转】ELK到底是什么鬼?辣么多公司用!
- JAVA代码(GET方式)请求URL(HTTP,HTTPS)
- SSH--完全分布式主机设置【克隆过安装过Hadoop的主机后】
- C++进阶--处理拷贝赋值运算符中自赋值的情况
- spark 非常好的学习内容
- session的理解和使用
- C++中特殊的宏定义
- Docker技术这些应用场景【转】
- 计算机音频基础-PCM简介
- 为WPF程序添加字体
热门文章
- tornado 路由系统----扩展(include)
- java3
- 错误:The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, MyFragment)
- Droidbox恶意软件动态分析环境搭建
- 原生js完成拼图小游戏
- C语言中do...while(0)的妙用(转载)
- 注解:【有连接表的】Hibernate双向1->;N关联(仅N端控制关联关系)
- Deep Learning模型之:CNN卷积神经网络(一)深度解析CNN
- 【maven】maven创建web项目-pom文件提示web.xml is missing and <;failOnMissingWebXml>; is set to true
- java导出word(带图片)