2013-03-04 2 views
0

나는 buzzer app for IOS에서 일하고 있습니다. click the buzzer이 울리면 30 seconds가 튀어 나오고 after 1까지 카운트 다운 한 다음 윙윙 거리며 사라집니다. 30-second countdown 과정에서 누군가가 부저를 재설정 (타이머가 꺼지고 사라지도록)하면 buzzer again을 클릭하면됩니다.타이머 재설정 문제 IOS

다음과 같은 세 가지 질문이 있습니다.
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 

답변

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]; 전화 시작 방법

2) seconds.alpha = 0.0와 IB 코드 또는 라벨()를 초기화, 라벨 용)

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

} 
+0

대를 만들기 위해! 이것은 많은 도움이됩니다. 질문 2, "리셋"에 의해 나는 그것이 켜지지 않는 기본 값으로 재설정되기를 원한다. 기본적으로 카운트 다운이 진행되지 않도록 버저를 취소하고 싶습니다. 어떻게해야합니까? 또한 부저가 한 사이클을 완료 한 후 다시 클릭하면 번호 매기기가 다음과 같이 진행됩니다 : "0, 29, 28, 27" 어떻게 해결할 수 있습니까? – oneintheword

+0

답을 찾았습니다. 타이머가 실행 중인지를 결정하는 if 문을 만들었습니다. 그렇다면 타이머를 무효로하고 nil로 설정하고 초를 숨기고 기본 int를 재설정하고 30 초로 초를 설정합니다. '- (IBAction) start : (id) 보낸 사람 { if (timer! = nil) { [timer invalidate]; timer = nil; seconds.hidden = YES; MainInt = 30; seconds.text = [NSString stringWithFormat : @ "30"]; } else { AudioServicesPlaySystemSound (buzzer); [self performSelector : @selector (buzzerDelay) withObject : nil afterDelay : 0.85]; } } – oneintheword

0

1 부저 지연에 표시되고 재설정. 그리고 ...이 보이지

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

2,3) 두 번째와 세 번째 질문은 시작 방법에 코드의 라인으로 해결 될 수있다 ...

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