NSTimers困境

时间:2011-10-28 09:10:46

标签: nstimer touchesbegan touches taps

以下是我为学生在学校创建的免费应用程序的一些代码。它非常简单,可以计算在十秒钟内点击屏幕的次数。我有一个计时器countDownTimer从3倒数到0,然后启动我的下一个计时器'myTimer'然后从10 - 0进行主倒计时。我的应用程序中的一切都很好用,除了触摸开始时第一个计时器已经启动,我只希望它在第二个计时器启动时工作(10秒钟)。

谁能看到我出错的地方?

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


}
}

1 个答案:

答案 0 :(得分: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];
    }
}