zoukankan      html  css  js  c++  java
  • swfit-计时器

    import UIKit
    
    class FourVC: UIViewController {
    
        var label:UILabel = UILabel()
        var index : Int = 1
        var timer:Timer = Timer()
        override func viewDidLoad() {
            super.viewDidLoad()
    
            label = UILabel.init()
            label.frame = CGRect.init(x:100,y:100,200 ,height:200)
            label.text = "计时器"
            label.textColor = UIColor.black
            self.view.addSubview(label)
            
            timer = Timer.scheduledTimer(timeInterval: 1, 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:250,y:300,100 ,height:50)
            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:50,y:300,100 ,height:50)
            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 += 1
            label.text = "(index)"
            
            if index == 11 {
                //关闭计时器
                timer.fireDate = NSDate.distantFuture
                label.text = "倒计时停止(index)"
            }
            
        }
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
  • 相关阅读:
    编写你的应用程序(二)、原生客户端模块
    编写你的应用程序(一)、应用结构
    checkpoint机制,show engine innodb status
    InnoDB关键特性,innodb_old_blocks_time,锁,内存管理,latch争用
    Innodb引擎,MySQL修改参数
    MySQL数据库体系结构
    IT行业数据库分析
    生成一个千万行的表
    范式小知识
    MySQL触发器
  • 原文地址:https://www.cnblogs.com/sayimba/p/6237532.html
Copyright © 2011-2022 走看看