2011-10-28 4 views
0

다음은 학교에서 제 학생을 위해 만드는 무료 앱 코드입니다. 매우 간단하며 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]; 


} 
} 

답변

0

그것은 tapCount를 사용하는 것 같아 당신이 원하는 정확한 될 수 없습니다 - 그것은 사용자가 특정 시점에 자신의 손가락을 탭

횟수를 반환

이며 이중 또는 3 중 탭과 같은 제스처를 감지 할 수 있습니다. 그것은 제스처 인식 시스템이 새로운 제스처라고 결정한 시점부터 시작됩니다. 대신 그

, 왜 당신은 속성에 자신의 카운터를 유지하지 않습니다 그리고

@property(assign)NSUInteger tapCount; 

당신의 touchesBegan:withEvent:에, 증가하는 카운터 :

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    if (myTimer != nil) { 
     self.tapCount = self.tapCount + 1; 
     tapStatus.text = [NSString stringWithFormat:@"%d taps", self.tapCount]; 
    } 
}