如何使按钮在整个计时器中出现和消失

时间:2013-08-11 23:28:02

标签: ios objective-c uibutton nstimer

我有这个应用程序,其中有一个计时器,我希望能够在该计时器的随机点显示一个按钮。排序就像一个游戏设置,你必须得到按钮,以便在它消失之前获得更多时间。我已经有了一些代码,但问题是我已经拥有的计时器搞砸了(我可以告诉它,因为它链接到一个标签。它搞砸了,因为它下降了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
}

2 个答案:

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

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

答案 1 :(得分: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,这意味着随机计时器将始终立即执行。