繁体   English   中英

重置计时器问题IOS

[英]Resetting Timer issues IOS

我正在buzzer app for IOS 当您click the buzzerclick the buzzer ,弹出30 seconds ,然后倒数直到after 1 ,然后click the buzzer消失。 如果在30-second countdown ,有人想要重置蜂鸣器(使计时器关闭并消失),他们将buzzer again单击buzzer again

我有三个问题:
1.如何以不可见的UILabel启动应用程序,然后在首次单击时显示?
2.如何在倒数30秒期间单击以重置蜂鸣器?
3.重置蜂鸣器是否可以解决多次单击时计时器快速关闭的问题?

视图控件

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

- (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

然后调用[seconds setHidden:NO]; 在启动方法中

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)为了使标签在延迟后变得可见,请使用seconds.alpha = 0.0初始化标签(在代码中或在IB中)。 然后使其可见...

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

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