我到处都看,但我找不到怎么做。我需要一个简单的计时器,可以在几毫秒内计数,并可以停止。 (XCode 3.1,Objective-C, iPhone OS开发)
答案 0 :(得分:2)
我只是愚蠢或者是“计时器”和秒表一样。
如果是,请查看此video tutorial
答案 1 :(得分:2)
这是我用一分钟,秒钟和毫秒秒做一个计时器的示例代码。所以,它对你有帮助。并且在不使用后也会释放..
-(void)showTime:(NSTimer *)sender
{
miniSeconds++;
if (miniSeconds == 10)
{
miniSeconds = 0;
seconds++;
if (seconds == 60)
{
seconds = 0;
minutes++;
}
}
timeLabel.text = [NSString stringWithFormat:@"%02i:%02i:%02i",minutes,seconds,miniSeconds];
}
-(void)nextLevelTime
{
[labelTimer invalidate];
miniSeconds = 0;
seconds = 0;
minutes = 0;
}
nextLevel方法用于停止计时器并释放所有东西..
使用它,我已完成此代码..
答案 2 :(得分:0)
答案 3 :(得分:0)
@interface ViewController()
{
UILabel *progress;
NSTimer *timer;
int currMinute;
int currSeconds;
int currHours;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
progress=[[UILabel alloc] initWithFrame:CGRectMake(80, 15, 100, 50)];
progress.textColor=[UIColor redColor];
[progress setText:@"Time : 00:00:00"];
progress.backgroundColor=[UIColor clearColor];
[self.view addSubview:progress];
currMinute=00;
currSeconds=00;
currHours=00;
// Do any additional setup after loading the view, typically from a nib.
}
-(void)start
{
timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
}
-(void)timerFired
{
currSeconds+=1;
if(currSeconds==60)
{
currSeconds=0;
currMinute+=1;
if(currMinute==60)
{
currMinute=0;
currHours+=1;
}
}
[_progress setText:[NSString stringWithFormat:@"%@%02d%@%02d%@%02d",@"Time : ",currHours,@":",currMinute,@":",currSeconds]];
if(currMinute==2)//stop at 2 minute
{
[timer invalidate];
}
}