简体   繁体   English

重置计时器问题IOS

[英]Resetting Timer issues IOS

I'm working on a buzzer app for IOS . 我正在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. 当您click the buzzerclick the buzzer ,弹出30 seconds ,然后倒数直到after 1 ,然后click the buzzer消失。 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 . 如果在30-second countdown ,有人想要重置蜂鸣器(使计时器关闭并消失),他们将buzzer again单击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? 1.如何以不可见的UILabel启动应用程序,然后在首次单击时显示?
2. How do you reset the buzzer by clicking it during the 30 second countdown? 2.如何在倒数30秒期间单击以重置蜂鸣器?
3. Will reseting the buzzer fix the problem of the timer going down extremely quick when clicked multiple times? 3.重置蜂鸣器是否可以解决多次单击时计时器快速关闭的问题?

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" 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 1)在启动时隐藏标签

- (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]; 然后调用[seconds setHidden:NO]; in start method 在启动方法中

2) reset buzzer 2)重置蜂鸣器

-(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 . 1)为了使标签在延迟后变得可见,请使用seconds.alpha = 0.0初始化标签(在代码中或在IB中)。 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... 2,3)您的第二个和第三个问题可以用start方法中的一行代码来解决...

-(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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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