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.
}
}

最新文章

  1. jquery css事件编程 位置 操作
  2. 模拟apache commons dbutils 实现自己的BeanListHandler(回调应用)
  3. PHP内核探索之变量(3)- hash table
  4. HTML 透明、阴影,圆角等知识点
  5. js方法在iframe父子窗口
  6. CF781D Axel and Marston in Bitland [倍增 矩阵乘法 bitset]
  7. CSS3总结(干货)
  8. MTK 使用iptable 命令来完成网络路由(android WIFI/4G分享网络)
  9. AX_xSession
  10. VC 中引用js文件
  11. 【转】ELK到底是什么鬼?辣么多公司用!
  12. JAVA代码(GET方式)请求URL(HTTP,HTTPS)
  13. SSH--完全分布式主机设置【克隆过安装过Hadoop的主机后】
  14. C++进阶--处理拷贝赋值运算符中自赋值的情况
  15. spark 非常好的学习内容
  16. session的理解和使用
  17. C++中特殊的宏定义
  18. Docker技术这些应用场景【转】
  19. 计算机音频基础-PCM简介
  20. 为WPF程序添加字体

热门文章

  1. tornado 路由系统----扩展(include)
  2. java3
  3. 错误:The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, MyFragment)
  4. Droidbox恶意软件动态分析环境搭建
  5. 原生js完成拼图小游戏
  6. C语言中do...while(0)的妙用(转载)
  7. 注解:【有连接表的】Hibernate双向1->N关联(仅N端控制关联关系)
  8. Deep Learning模型之:CNN卷积神经网络(一)深度解析CNN
  9. 【maven】maven创建web项目-pom文件提示web.xml is missing and <failOnMissingWebXml> is set to true
  10. java导出word(带图片)