【问题标题】:NSTimers dilemmaNSTimers 困境
【发布时间】:2011-10-28 09:10:45
【问题描述】:

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


}
}

【问题讨论】:

    标签: nstimer touchesbegan touches taps


    【解决方案1】:

    似乎使用 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];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-04-23
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 2017-10-08
      • 2016-02-04
      • 2012-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多