0

私はに取り組んでいbuzzer app for IOSます。するとclick the buzzer、ブザーが鳴り、30 secondsポップアップし、カウントダウンしてからafter 1ブザーが鳴り、消えます。の途中で30-second countdown誰かがブザーをリセットしたい場合 (タイマーが鳴って消えるようにするため) は、 をクリックするだけbuzzer againです。

3 つの質問が
あります。1. UILabel を非表示にしてアプリを起動し、最初のクリックで表示するにはどうすればよいですか?
2. 30 秒のカウントダウン中にブザーをクリックしてリセットするにはどうすればよいですか?
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"

@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) 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 に答える