2011-10-28 23 views
0

下面是我爲學生在學校創建的免費應用程序的一些代碼。這非常簡單,並計算他們在十秒內點擊屏幕的次數。我有一個計時器countDownTimer,它從3減少到0,然後啓動我的下一個定時器'myTimer',然後從10 - 0進行主倒計時。除了第一個定時器已經啓動出發,我只希望它在第二次出發時(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]; 
    } 
}