1. 程式人生 > >ios NSTimer做的計時器(精確到微妙)

ios NSTimer做的計時器(精確到微妙)

自己在學習過程中用到過NSTimer,當時在網上找了不少資料。現在自己做一個簡單的demo跟大家分享。

我就直接在下面貼上程式碼,希望能幫到一些初學iOS的朋友。

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{

UIButton *startButton;

UIButton *stopButton;

UILabel *timeLabel;

UILabel *detailLabel;

NSTimer *mytimer;

}

@property (nonatomic

) BOOL isOn;

@property (nonatomic) int myTimerecord,hour,min,scd,mscd;

@property (nonatomic) NSString *hourText,*minText,*scdText,*mscdText,*timeLabelText;

@end


ViewController.m:

#import "ViewController.h"

@interfaceViewController ()

@end

@implementation ViewController

_hour = 0;

_min = 0;

_scd =

0;

_mscd = 0;

_myTimerecord = 0;

- (void)viewDidLoad {

    [superviewDidLoad];

detailLabel = [[UILabelalloc]initWithFrame:CGRectMake(60, 30, 200, 70)];

detailLabel.text = @"計時器";

    [detailLabelsetTextAlignment:NSTextAlignmentCenter];

    [self.viewaddSubview:detailLabel];

startButton = [[UIButtonalloc]

initWithFrame:CGRectMake(60, 320, 60, 40)];

    [startButtonsetBackgroundColor:[UIColorblueColor]];

    [startButtonsetTitle:@"開始"forState:UIControlStateNormal];

    [startButtonaddTarget:selfaction:@selector(startBtAction) forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:startButton];

stopButton = [[UIButtonalloc]initWithFrame:CGRectMake(200, 320, 60, 40)];

    [stopButtonsetBackgroundColor:[UIColorblackColor]];

    [stopButtonsetTitle:@"停止"forState:UIControlStateNormal];

    [stopButtonaddTarget:selfaction:@selector(stopBtAction) forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:stopButton];

timeLabel = [[UILabelalloc]initWithFrame:CGRectMake(0, 100, 320, 120)];

    [timeLabelsetTextAlignment:NSTextAlignmentCenter];

    [timeLabelsetText:@"00  :  00  :  00  :  00"];

timeLabel.font = [UIFontboldSystemFontOfSize:32.0];

    [timeLabelsetBackgroundColor:[UIColorblackColor]];

    [timeLabelsetTextColor:[UIColorwhiteColor]];

    [self.viewaddSubview:timeLabel];

//這裡0.01表示設定時間間隔是0.01

mytimer = [NSTimerscheduledTimerWithTimeInterval:0.01target:selfselector:@selector(timerFired) userInfo:nilrepeats:YES];

// Do any additional setup after loading the view.

}

//根據上面的設定,這個方法每隔0.01秒執行一次

-(void)timerFired

{

if (_isOn) {

_myTimerecord++;                      //記錄累計微妙的值,根據這個值計算出相對應的秒,分鐘,小時

_hour = _myTimerecord / 360000;

_min = (_myTimerecord%360000)/6000;

_scd = (_myTimerecord%6000)/100;

_mscd = _myTimerecord%100;

    }

    [selfupdateUI];

}

-(void)startBtAction

{

if (_isOn) {

_isOn = NO;

    } else {

_isOn = YES;

    }

}

-(void)stopBtAction

{

_isOn = NO;//暫停

    [selfrealStop];

}

-(void)realStop

{

_hour = 0;

_min = 0;

_scd = 0;

_mscd = 0;

_myTimerecord = 0;

_isOn = NO;

}

//這個方法實現資料(_myTimerecord)和頁面中顯示的值是統一的

-(void)updateUI

{

if (_hour < 10) {

_hourText = [[NSStringalloc]initWithFormat:@"0%d",_hour];

    }else {

_hourText = [[NSStringalloc]initWithFormat:@"%d",_hour];

    }

if (_min < 10) {

_minText = [[NSStringalloc]initWithFormat:@"0%d",_min];

    }else {

_minText = [[NSStringalloc]initWithFormat:@"%d",_min];

    }

if (_scd < 10) {

_scdText = [[NSStringalloc]initWithFormat:@"0%d",_scd];

    }else {

_scdText = [[NSStringalloc]initWithFormat:@"%d",_scd];

    }

if (_mscd < 10) {

_mscdText = [[NSStringalloc]initWithFormat:@"0%d",_mscd];

    } else {

_mscdText = [[NSStringalloc]initWithFormat:@"%d",_mscd];

    }

_timeLabelText = [[NSStringalloc]initWithFormat:@"%@  :  %@  :  %@  :  %@",_hourText,_minText,_scdText,_mscdText];

    [timeLabelsetText:_timeLabelText];

if (_isOn) {

        [startButtonsetTitle:@"暫停"forState:UIControlStateNormal];

    }else {

        [startButtonsetTitle:@"開始"forState:UIControlStateNormal];

    }

}

@end



相關推薦

ios NSTimer計時精確微妙

自己在學習過程中用到過NSTimer,當時在網上找了不少資料。現在自己做一個簡單的demo跟大家分享。 我就直接在下面貼上程式碼,希望能幫到一些初學iOS的朋友。 ViewController.h: #import <UIKit/UIKit.h> @int

iOS 單例計時頁面切換仍然計時

//–––––––––––––––––––––單例.h–––––––––––––––––––––––– #import <Foundation/Foundation.h> @in

【Java】Java計時秒表

sna win8 pbo tcl constant 源代碼 AS add 開始時間 https://blog.csdn.net/c_jian/article/details/50506759 應用名稱:Java計時器 用到的知識:Java GUI編程 開發環境:win8+e

jQuery 計時jquery timers簡單應用

/** * jQuery.timers - Timer abstractions for jQuery * Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com) * Licensed under the WTFPL (htt

iOS與EV3混合機器人編程系列之二】工欲善其事,必先利其準備篇

style 混合 版權 相同 開發 code 操作系統 圖形 ipa 在上一篇文章中,我們論述了iOS與EV3結合後機器人開發的無限可能。那麽,大家要不要一起來Hacking一把呢?為了能夠完整地完畢我接下來我講的項目。我們須要做下面準備:1、一臺Mac執行MAC OS

SpringCloud利用閘道攔截Token驗證JWT方式 SpringCloud利用閘道攔截Token驗證JWT方式

SpringCloud利用閘道器攔截做Token驗證(JWT方式) 2018年09月29日 15:51:50 19八9 閱讀數:23 更多

iOS獲取WIFI配置資訊,WIFI名稱、閘道路由器地址、本機IP地址、DNS

DEMO地址(看程式碼,什麼就都有了):https://github.com/SimpleBraver/test_wifi.git 1、獲取當前手機WIFI名稱資訊 #import <SystemConfiguration/CaptiveNetwork.h> //iOS獲取當前手

進擊的小白Day014——計時

今天因為老師那邊例會需要,就自己diy了一個例會計時器。 收穫: 鍵入指定按鍵中斷迴圈,不用scanf,下面語句是鍵入Esc來中斷迴圈的 while (c != 27) { statement if (kbhit()) { c

Unity計時實現暫停,繼續等功能

       計時器是遊戲開發的一個非常常用的功能,簡單計時器的實現方式有很多中,可以每幀給一個float變數加Time.deltaTime,到一定時間的時候呼叫某個函式。也可以使用unity的協程和InvokeRepeating。但是簡單的使用這些組合呼叫起

Tensorflow學習筆記--使用遷移學習自己的影象分類Inception v3

本文主要使用inception v3的模型,再後面接一個softmax,做一個分類器。具體程式碼都是參照tf github。 整體步驟: 步驟一:資料準備,準備自己要分類的圖片訓練樣本。 步驟二:retrain.py 程式,用於下載inception v3模型及訓練後面的

C++ MFC 計時OnTimer的用法例項

1、”點選“跳轉介面的程式碼: // TODO:  在此新增控制元件通知處理程式程式碼  MyLog td;  td.DoModal(); 2、點選後,在另一個介面中顯示如下介面,文字框中的數字依次升高 (1)在介面中新增文字框,計時器 在視窗中右鍵 (2)新增文字框

iOS【NSTimer_計時

先看效果 程式碼 #import "NYViewController.h" @interface NYViewController () <UIAlertViewDelegate> @property (weak, nonatomic) IBOut

瘋狂ios講義之選擇UIPickerView

UIPickerView也是一個選擇器控制元件,它比UIDatePicker更加通用,它可以生成單列的選擇器,也可生成多列的選擇器,而且開發者完全可以自定義選擇項的外觀,因此用法非常靈活。 UIPickerView直接繼承了UIView,沒有繼承UIControl,因此,它不能像UIControl那樣繫結

命令行顏色換算基於python

documents image document class clas profile 修改 span eight import sys print(hex(int(sys.argv[1])<<16|int(sys.argv[2])<<8|int(

100行代碼實現最簡單的基於FFMPEG+SDL的視頻播放SDL1.x【轉】

工程 全屏 升級版 gin avcodec ive 系列文章 相同 hello 轉自:http://blog.csdn.net/leixiaohua1020/article/details/8652605 版權聲明:本文為博主原創文章,未經博主允許不得轉載。

jquery+bootstrap一個開關互斥選擇

utf jquer class role 選擇 span chrome query back 知識點:js中的return 不能使js程序停止或者跳出執行,會繼續往下執行。要想讓js程序停止或者跳出 可以用 return false 就可以了。 代碼: <!-----

使用Nginx+CppCMS構建高效Web應用服務之三

影響 size ppc light .html 數據顯示 com logs cppcms 使用Nginx+CppCMS構建高效Web應用服務器(之三)   --充分利用服務器和客戶端計算能力              歡迎測試,攻擊:http://www.lite

java 構造 構造方法

fin {} 相同 java 開發 height private 實例 數據類型 public 構造器 (constructor,構造方法) 3W what ? why? how? 是什麽 , 為什麽, 怎麽用? 註意事項? What ? 是什麽: 構造器(又叫構造方法)本

本地配置DNS服務MAC版

希望 設備 gin host bin 方式 regexp update 服務 作為一個前端開發者,會遇到使用cookie的情況,常見的如:登錄,權限控制,視頻播放,圖形驗證碼等,這時候本地開發者在PC上會使用修改hosts的方式添加指向本地的域名,來獲取cookie的同域名

Spring MVC-視圖解析View Resolverr-內部資源查看解析Internal Resource View Resolver示例轉載實踐

ast mes 技術 子文件夾 schema html cati href tree 以下內容翻譯自:https://www.tutorialspoint.com/springmvc/springmvc_internalresourceviewresolver.htm 說