2011-10-28 6 views
0

Voici une partie du code pour une application gratuite que je crée pour mes élèves à l'école. C'est très simple et compte le nombre de fois qu'ils tapent l'écran dans les dix secondes. J'ai une minuterie countDownTimer qui décompte de 3 à 0 puis déclenche mon prochain minuteur "myTimer" qui fait ensuite le compte à rebours principal de 10 - 0. Tout fonctionne très bien dans mon application sauf que les touches ont commencé à démarrer lorsque le premier minuteur a été déclencher et je veux seulement que cela fonctionne quand la seconde a été déclenchée (pour les 10 secondes).NSTimers dilemme

Quelqu'un peut-il voir où je me suis trompé?

#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]; 


} 
} 

Répondre

0

Il semble que l'utilisation tapCount ne va pas être précis pour ce que vous voulez - il retourne

le nombre de fois que l'utilisateur tapé leurs doigts sur un certain point

et il est destiné à détecter un geste comme un double ou triple tap. Cela commence au point où le système de reconnaissance gestuelle décide que c'est un nouveau geste.

Au lieu de cela, pourquoi gardez-vous pas votre propre compteur dans une propriété:

@property(assign)NSUInteger tapCount; 

Ensuite, dans votre touchesBegan:withEvent:, incrémenter ce compteur:

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