我正在研究一个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