0

以下是我在学校为我的学生创建的免费应用程序的一些代码。它非常简单,可以计算他们在十秒内点击屏幕的次数。我有一个计时器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];


}
}
4

1 回答 1

0

似乎 usingtapCount不会准确地满足您的需求-它会返回

用户在某个点上敲击手指的次数

它的目的是检测像双击或三击这样的手势。它从手势识别系统确定它是一个新手势的那一刻开始。

取而代之的是,您为什么不将自己的柜台放在财产中:

@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];
    }
}
于 2011-10-28T17:44:46.343 回答