简体   繁体   中英

Resetting Timer issues IOS

I'm working on a buzzer app for IOS . When you click the buzzer , it buzzes, 30 seconds pops up, and counts down until after 1 and then buzzes and disappears. If, during the course of the 30-second countdown , someone wants to reset the buzzer (so that the timer goes off and disappears), they will just click the buzzer again .

I have three questions:
1. How do you start the app with the UILabel invisible and then shows up upon the first click?
2. How do you reset the buzzer by clicking it during the 30 second countdown?
3. Will reseting the buzzer fix the problem of the timer going down extremely quick when clicked multiple times?

viewcontrol.h

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>

@interface ViewController : UIViewController {

    IBOutlet UILabel *seconds;
    NSTimer *timer;
    int MainInt;
    SystemSoundID buzzer;

}


- (IBAction)start:(id)sender;
- (void)countdown;

@end

ViewController.m #import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


-(IBAction)start:(id)sender {
   seconds.hidden = NO;
   MainInt = 30;
   timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countdown) userInfo:nil repeats:YES];
AudioServicesPlaySystemSound(buzzer);

}


-(void)countdown {
    MainInt -= 1;
    seconds.text = [NSString stringWithFormat:@"%i", MainInt];
    if (MainInt < 1) {
        [timer invalidate];
        timer = nil;
        seconds.hidden = YES;
        AudioServicesPlaySystemSound(buzzer);
        }


}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSURL *buttonURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Buzz" ofType:@"wav"]];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)buttonURL, &buzzer);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

1) hiding the label on startup

- (void)viewDidLoad
{

   [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //hide the label initially
    [seconds setHidden:YES];

    /// other code

then call [seconds setHidden:NO]; in start method

2) reset buzzer

-(IBAction)start:(id)sender {
   seconds.hidden = NO;
   MainInt = 30;

   if(timer != nil)
   {
      //timer exist..stop previous timer first.
     [timer invalidate];
   }

   timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countdown) userInfo:nil repeats:YES];
AudioServicesPlaySystemSound(buzzer);

}

1) For the label becoming visible on a delay, initialize the label (in code or in IB) with seconds.alpha = 0.0 . Then to make it visible...

NSTimeInterval delayUntilLabelIsVisible = 3.0;  // 3 seconds
[UIView animateWithDuration:0.3
                      delay:delayUntilLabelIsVisible
                    options:UIViewAnimationCurveEaseIn
                 animations:^{seconds.alpha = 1.0;}
                 completion:^(BOOL finished) {}];

2,3) Your second and third questions can be solved with a line of code in the start method...

-(IBAction)start:(id)sender {

   seconds.hidden = NO;
   MainInt = 30;

   // cancel the old timer before creating a new one
   // (otherwise many timers will run at once, calling the buzzer method too often)
   [timer invalidate];

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM