0

我正在研究一个buzzer app for IOS. 当您 时click the buzzer,它会发出嗡嗡声、30 seconds弹出并倒计时,直到after 1嗡嗡声消失。如果在 的过程中30-second countdown,有人想要重置蜂鸣器(这样计时器就会响起并消失),他们只需点击buzzer again

我有三个问题:
1.如何在 UILabel 不可见的情况下启动应用程序,然后在第一次单击时显示?
2. 30秒倒计时时点击蜂鸣器如何复位?
3. 重置蜂鸣器是否可以解决多次点击时定时器非常快的问题?

视图控制.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
4

2 回答 2

0

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];start 方法

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

}
于 2013-03-04T04:59:41.167 回答
0

1) 对于延迟可见的标签,用 初始化标签(在代码中或在 IB 中)seconds.alpha = 0.0。然后让它可见...

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);
}
于 2013-03-04T05:15:29.087 回答