自己在学习过程中用到过NSTimer,当时在网上找了不少资料。现在自己做一个简单的demo跟大家分享。
我就直接在下面粘贴代码,希望能帮到一些初学iOS的朋友。
ViewController.h:
#import
@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"
@interface ViewController ()
@end
@implementation ViewController
_hour = 0;
_min = 0;
_scd = 0;
_mscd = 0;
_myTimerecord = 0;
- (void)viewDidLoad {
[super viewDidLoad];
detailLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 30, 200, 70)];
detailLabel.text = @"计时器";
[detailLabel setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:detailLabel];
startButton = [[UIButton alloc]initWithFrame:CGRectMake(60, 320, 60, 40)];
[startButton setBackgroundColor:[UIColor blueColor]];
[startButton setTitle:@"开始" forState:UIControlStateNormal];
[startButton addTarget:self action:@selector(startBtAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:startButton];
stopButton = [[UIButton alloc]initWithFrame:CGRectMake(200, 320, 60, 40)];
[stopButton setBackgroundColor:[UIColor blackColor]];
[stopButton setTitle:@"停止" forState:UIControlStateNormal];
[stopButton addTarget:self action:@selector(stopBtAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:stopButton];
timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 120)];
[timeLabel setTextAlignment:NSTextAlignmentCenter];
[timeLabel setText:@"00 : 00 : 00 : 00"];
timeLabel.font = [UIFont boldSystemFontOfSize:32.0];
[timeLabel setBackgroundColor:[UIColor blackColor]];
[timeLabel setTextColor:[UIColor whiteColor]];
[self.view addSubview:timeLabel];
//这里0.01表示设置时间间隔是0.01秒
mytimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timerFired) userInfo:nil repeats: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;
}
[self updateUI];
}
-(void)startBtAction
{
if (_isOn) {
_isOn = NO;
} else {
_isOn = YES;
}
}
-(void)stopBtAction
{
_isOn = NO;//暂停
[self realStop];
}
-(void)realStop
{
_hour = 0;
_min = 0;
_scd = 0;
_mscd = 0;
_myTimerecord = 0;
_isOn = NO;
}
//这个方法实现数据(_myTimerecord)和页面中显示的值是统一的
-(void)updateUI
{
if (_hour < 10) {
_hourText = [[NSString alloc]initWithFormat:@"0%d",_hour];
}else {
_hourText = [[NSString alloc]initWithFormat:@"%d",_hour];
}
if (_min < 10) {
_minText = [[NSString alloc]initWithFormat:@"0%d",_min];
}else {
_minText = [[NSString alloc]initWithFormat:@"%d",_min];
}
if (_scd < 10) {
_scdText = [[NSString alloc]initWithFormat:@"0%d",_scd];
}else {
_scdText = [[NSString alloc]initWithFormat:@"%d",_scd];
}
if (_mscd < 10) {
_mscdText = [[NSString alloc]initWithFormat:@"0%d",_mscd];
} else {
_mscdText = [[NSString alloc]initWithFormat:@"%d",_mscd];
}
_timeLabelText = [[NSString alloc]initWithFormat:@"%@ : %@ : %@ : %@",_hourText,_minText,_scdText,_mscdText];
[timeLabel setText:_timeLabelText];
if (_isOn) {
[startButton setTitle:@"暂停" forState:UIControlStateNormal];
}else {
[startButton setTitle:@"开始" forState:UIControlStateNormal];
}
}
@end