下面是我为我学校的学生创建的一个免费应用程序的一些代码。它非常简单,可以计算用户在10秒内点击屏幕的次数。我有一个计时器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];
}
}发布于 2011-10-29 01:44:46
似乎使用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];
}
}https://stackoverflow.com/questions/7927254
复制相似问题