0

我有这个应用程序,其中有一个计时器,我希望能够在该计时器的随机点显示一个按钮。有点像游戏设置,你必须让按钮在它消失之前获得更多时间。我已经有一些代码,但问题是我已经拥有的计时器被搞砸了(我可以说是因为它链接到一个标签。它搞砸了,因为它下降了 2 而不是 1 并且这只发生在我有使按钮随计时器出现和消失的代码。

这是我的计时器被搞砸的代码:

-(IBAction)Ready:(id)sender {

    [self performSelector:@selector(TimerDelay) withObject:nil afterDelay:1.0];
    [self performSelector:@selector(randomTimer) withObject:nil afterDelay:0.5];


}

-(void)TimerDelay {

    MainInt = 36;

    timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                             target:self
                                           selector:@selector(countDownDuration)
                                           userInfo:nil
                                            repeats:YES];
}

这是按钮的代码:

    -(IBAction)Ready:(id)sender { //the same ready action as above

        [self performSelector:@selector(TimerDelay) withObject:nil afterDelay:1.0];
        [self performSelector:@selector(randomTimer) withObject:nil afterDelay:0.5];


    }

-(void)TimerDelay {

    MainInt = 36;

    timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                             target:self
                                           selector:@selector(countDownDuration)
                                           userInfo:nil
                                            repeats:YES];
}

- (void)randomTimer {

    NSInteger randomTime = arc4random_uniform(0); //Some number between 0 and 9

    timer = [NSTimer scheduledTimerWithTimeInterval:randomTime
                                                  target:self
                                                selector:@selector(showButton)
                                                userInfo:nil
                                                 repeats:NO];
}


- (void)showButton {

    if(self.plus5.hidden == YES) {

        self.plus5.hidden = NO;
    }
    else {

        self.plus5.hidden = YES;
    }

    [self TimerDelay]; //Call random timer function again to run this method at a future random time
}
4

2 回答 2

2

您一个接一个地调用“randomTimer”和“TimerDelay”(这应该是“timerDelay”)并调度和重新调度“定时器”。

我认为你应该重新考虑你的架构,在这里......

编辑:只使用一个计时器,让它管理你的按钮。

- (void)somewhereInYourCode
{
    shouldShowButton = YES;

    timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                             target:self
                                           selector:@selector(timeFired)
                                           userInfo:nil
                                            repeats:YES];
}

- (void)timeFired
{
    // Here your countdown and...

    if (shouldShowButton) {
        NSInteger randomTimeInt = arc4random_uniform(9); // as suggested by Bergasms

        float randomTimeFloat = randomTimeInt;

        [self performSelector:@selector(showButton) withObject:nil afterDelay:randomTimeFloat];

        shouldShowButton = NO;
    }
}

这样你的计时器就不会(可能)搞砸了。

于 2013-08-11T23:40:54.647 回答
1

这个:

    arc4random_uniform() will return a uniformly distributed random number
 less than upper_bound.

是你的问题之一。

NSInteger randomTime = arc4random_uniform(0); //Some number between 0 and 9    

需要是

NSInteger randomTime = arc4random_uniform(9); //Some number between 0 and 9

我不能说你的其余代码。但是您拥有的行将始终返回 0,这意味着随机计时器将始终立即执行。

于 2013-08-11T23:31:36.353 回答