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