2011-10-28 7 views
0

以下は、私が学校で生徒のために作成している無料アプリのコードの一部です。非常にシンプルで、10秒以内に画面をタップする回数を数えます。私は1つのタイマーcountDownTimerが3から0までカウントダウンし、次に私の次のタイマー 'myTimer'を設定してから10 - 0のメインカウントダウンを行います。私はそれが2番目に(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を使用してのように思えるが、あなたが望むもののために正確であることを行っていない - それは、ユーザが特定のポイントに自分の指をタップ

回数を返し

とは、ダブルタップやトリプルタップのようなジェスチャーを検出することを意味します。ジェスチャ認識システムが新しいジェスチャであると判断した時点から開始されます。その代わりに

、なぜあなたはプロパティで独自のカウンタを保持しません。

@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]; 
    } 
}