Question

Hello I recently added a timer in my app so when the user clicks the balloon it starts the timer but I also have a tap counter hooked to the button as well. So heres my issue when I press the balloon multiple times the tap counter works and so does the timer but when start to tap faster the timer becomes stuck in a loop and when I stop tapping it, it counts down past zero into negatives really fast

How do I fix this?

    - (IBAction)yourbuttonClicked1:(UIButton *)sender
    {


   //start a background sound
   NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"tap" ofType:   
    @"wav"];
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath ];
   myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil ];
  [myAudioPlayer play];

     //for Switch view
    ViewController2 *nextView = [[ViewController2 alloc] initWithNibName:@"ViewController" 
  bundle: nil];
  [self.navigationController pushViewController:nextView animated:YES];

This is where the timer code is I think it might be conflicting with the tap counter?

  myTime = 60;
  countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self    
 selector:@selector(countDown) userInfo:nil repeats:YES];

 }



 -(void)countDown {

myTime = myTime;
countdownLabel.text = [NSString stringWithFormat:@"%i", myTime];

if (myTime == 0) {

    [countdownTimer invalidate];

  }


 }

This is where the timer code ends!

  - (void) saveNumberOfTaps:(int)numberOfTaps {
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInt:numberOfTaps]   
 forKey:@"numberOfTaps"];
NSLog(@"Saved numberOfTaps: %i", numberOfTaps);
}



 - (int) getNumberOfTaps {
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"numberOfTaps"] intValue]) {
    int numberOfTaps = [[[NSUserDefaults standardUserDefaults]   
   objectForKey:@"numberOfTaps"] intValue];
    NSLog(@"Getting numberOfTaps: %i", numberOfTaps);
    return numberOfTaps;
}
else {
    NSLog(@"No taps saved yet");
    return 0;

    }
 }
Était-ce utile?

La solution

Try to modify your timer initialization in such way:

 myTime = 60;

[countdownTimer invalidate];
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self    
 selector:@selector(countDown) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:countdownTimer forMode:NSRunLoopCommonModes];

Autres conseils

Check your -countDown method:

myTime = myTime;

Is it typo?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top