다음은 학교에서 제 학생을 위해 만드는 무료 앱 코드입니다. 매우 간단하며 10 초 이내에 화면을 탭하는 횟수를 계산합니다. 나는 하나의 타이머 countDownTimer
을 3에서 0으로 카운트 한 다음 내 다음 타이머 'myTimer'를 설정하고 10 - 0에서 주 카운트 다운을합니다. 모든 것이 제 1 타이머가 시작될 때 시작되는 터치를 제외하고 내 앱에서 훌륭하게 작동합니다 두 번째 설정이 끝나면 (10 초 동안) 작동하기를 원합니다.NSTimers 딜레마
어디서 잘못 본 사람이 있습니까?
#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];
}
}