Sunday, 11 August 2013

How to make a button appear and disappear throughout a timer

How to make a button appear and disappear throughout a timer

I have this app which has a timer in it and I want to be able to display a
button at random points in that timer. Sort Of like a game setting where
you have to get the button to get more time before it disappears. I
already have some code , but the problem is that the timer that i already
have is being messed up ( i can tell because it is linked to a label. It
messed up because it goes down by 2 instead of 1 and this only happens
when i have the code to make the button appear and disappear with the
timer.
Here is the code to my timer that is being messed up:
-(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];
}
Here is the code to the button:
-(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
}

No comments:

Post a Comment