以下是我在学校为我的学生创建的免费应用程序的一些代码。它非常简单,可以计算他们在十秒内点击屏幕的次数。我有一个计时器countDownTimer
,它从 3 倒数到 0,然后启动我的下一个计时器“myTimer”,然后从 10 - 0 开始主要倒计时。在我的应用程序中一切正常,除了在第一个计时器设置时开始触摸关闭,我只希望它在第二个已关闭(10 秒)时工作。
谁能看到我哪里出错了?
#import "newgameViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import "ViewController.h"
@implementation newgameViewController
@synthesize tapStatus, score, time, countDown;
-(IBAction)start {
[myTimer invalidate];
score.text= @"";
time.text= @"10";
tapStatus.text= @"";
[countDownTimer invalidate];
countDownTimer = nil;
countDown.text= @"3";
countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self
selector:@selector(showActivityCountDown) userInfo:nil repeats:YES];
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef =CFBundleCopyResourceURL(mainBundle,
(CFStringRef) @"beep", CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
-(IBAction)stop{
[myTimer invalidate];
myTimer = nil;
[countDownTimer invalidate];
countDownTimer = nil;
countDown.text= @"3";
}
-(IBAction)reset {
[myTimer invalidate];
myTimer = nil;
score.text= @"";
time.text= @"10";
tapStatus.text= @"";
[countDownTimer invalidate];
countDownTimer = nil;
countDown.text= @"3";
}
-(void)showActivityCountDown {
int currentTimeCount = [countDown.text intValue];
int newTimeCount = currentTimeCount - 1;
countDown.text = [NSString stringWithFormat:@"%d", newTimeCount];
if(currentTimeCount == 3)
{
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef =CFBundleCopyResourceURL(mainBundle,
(CFStringRef) @"beep", CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
else if(currentTimeCount == 2)
{
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef =CFBundleCopyResourceURL(mainBundle,
(CFStringRef) @"beep", CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
else if(currentTimeCount == 1)
{
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef =CFBundleCopyResourceURL(mainBundle,
(CFStringRef) @"beep", CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
[countDownTimer invalidate];
countDownTimer = nil;
countDown.text= @"Go!";
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self
selector:@selector(showActivity) userInfo:nil repeats:YES];
}
}
-(void)showActivity {
float currentTime = [time.text floatValue];
float newTime = currentTime - 0.1;
time.text = [NSString stringWithFormat:@"%.1f", newTime];
if(currentTime == 0.0)
{
[myTimer invalidate];
myTimer = nil;
time.text= @"STOP!";
score.text = tapStatus.text;
}
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (myTimer != nil) {
NSUInteger tapCount = [[touches anyObject] tapCount];
tapStatus.text = [NSString stringWithFormat:@"%d taps", tapCount];
}
}