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,width: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,width: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,width: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.
    }
}
时间: 2024-08-28 22:21:43

swfit-计时器的相关文章

计时器开始和关闭

<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>计时器</title></head><script type="text/javascript"> var num=0; var i; function

【效率】专为Win7系统设计的极简番茄计时器 - MiniPomodoro (附源码)

时光飞逝,一转眼坚持使用番茄工作法已经快3年了!能坚持这么长时间,主要还是得益于它的简单.但是令人纠结的是,这么长时间以来,换了7款不同的番茄计时器,仍然没有找到非常满意的: ■ 机械的噪音太大,会妨碍身边的同事,只能家里用.但是家里又太安静了,一旦响铃就跟晴天霹雳似的,把自己吓一跳. ■ 手机上的计时器 app 种类繁多,有偏重视觉效果的,有偏重任务管理的,有主打简单易用的,但是它们都有一个共同的缺点:不能保持屏幕常亮(太费电),时常会把它给忘了.另外怕影响同事一般都会把铃声关掉,只靠震动的话

iOS:三种常见计时器(NSTimer、CADisplayLink、dispatch_source_t)的使用

一.介绍 在iOS中,计时器是比较常用的,用于统计累加数据或者倒计时等,例如手机号获取验证码.计时器大概有那么三种,分别是:NSTimer.CADisplayLink.dispatch_source_t 二.使用 @property (strong,nonatomic)NSTimer *timer; @property (strong,nonatomic)CADisplayLink *displaylinkTimer; @property (strong,nonatomic)dispatch_s

让可等待的计时器添加APC调用

1 // TimerAPCRoutine.cpp : 定义控制台应用程序的入口点. 2 // 3 4 #include "stdafx.h" 5 #include <windows.h> 6 #include <iostream> 7 #include <time.h> 8 9 10 using namespace std; 11 12 13 void GetSystemTime(); 14 VOID CALLBACK TimerAPCRoutinu

Date应用计时器和倒计时

计时器应用 目的:通过date设置一个计时器,实现图一效果,点击按钮开始计时,小时数.分钟数.秒数分别放到不同的表格中显示,中间用冒号相隔. 思路:第一步布局,要有3个input标签用来放置小时数.分钟数.秒数:要设置i.j.k三个变量来放置这三个value值,要有一个button按钮用来触发点击事件,还要一个计时器setInterval(function(){},1000);用来计时. 注意事项:为放置点击多次按钮导致的同时触发多个计时器累加的事情,我们需要提前声明 var oTime=nul

分秒计时器

改一个效果,将纯秒的计时器更改为分秒的,出现了当59秒的时候,会出现1:59的样子. 利用下面的代码解决了问题 var sec=0; var t=null; var min=0; function timedCount(){sec=sec+1; t=window.setTimeout("timedCount()",1000); if(sec>59){min=0; min++; $('#').html(min); $('#').html(sec); } else{ $("

RDTSC指令实现微秒级计时器

//!微秒级别的计时器 //ExactTimer.h #pragma once #include <math.h> BOOL ReadDwordKey(IN HKEY hRootKey,IN LPCTSTR lpSubKey, IN LPCTSTR lpKey,OUT DWORD& dwValue){ HKEY hk; if (ERROR_SUCCESS!=RegOpenKey(hRootKey,lpSubKey,&hk)) return FALSE; DWORD dwType

ios开发之oc-NSTimer计时器简单使用

在游戏开发中,很多时候会用到一定时间内自动干嘛的效果,比如每隔一秒自动增加一个元素.很显然,我就用到了.object-c中的NSTimer就能实现这种效果. 1.定义NSTimer @property(nonatomic)NSTimer *MouseTimer;//计时器 -------------------------------------------------------------------------------------------------------------- 2.

JS详解Date应用+定时器原理+计时器案例

我们先说一下定时器吧: //定时器:设置一个定时器,再设置一个等待的时间,到达指定时间后,执行对应的操作//两种定时器:用法一样,区别一个执行后不会停下来,一个只执行一次//window.setInterval([function],[interval]);/*设置一个定时器,到达指定时间[interval] 执行我们的操作[function],然后定时器并没有停止,以后每隔这么长时间,都重新执行我们的function*/ //window.setTimeout([function],[inte

Android应用增加计时器

昨天写的Sudoku游戏需要增加计时器功能,使用Chronometer实现如下,由于Chronometer自己在调用stop之后后台的计时器还会继续增加,所以暂停功能需要额外实现: 在StartActivity onCreate方法中添加如下代码: textView = (TextView) findViewById(R.id.time_text); timer = (Chronometer) findViewById(R.id.chronometer); timer.setBase(Syste