2012-09-14 4 views
0

단추에 타이머를 설정하는 데 도움이 필요합니다. 지금은 내가 생각 NSTimer하지만 방법을 삽입해야, 나는사용자가 단추를 누르기 전에 타이머를 설정하십시오.

- (IBAction)btnPlaySound:(id)sender { 
    CFBundleRef mainBundle = CFBundleGetMainBundle(); 
    CFURLRef soundFileUrlRef; 
    soundFileUrlRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"", CFSTR(""), NULL); 
    UInt32 soundID; 
    AudioServicesCreateSystemSoundID(soundFileUrlRef, &soundID); 
    AudioServicesPlaySystemSound(soundID); 
} 

,이 코드를 가지고 있고 사용자가 한 번이 버튼을 2 초마다 활용할 수 있다는 싶어?

감사하기 위해 위의 코드가 작동하려면, 당신은 선언 "을 MyTimer"과 UIButton "을 myButton"라는 이름의 NSTimer가 필요합니다에서

+2

왜 한 경우에는'(CFStringRef) @ "단어"를 사용하고 다른 한 경우에는'CFSTR ("mp3")'을 사용합니까? 일관성을 유지해야합니다. 둘 다 문자열 리터럴이므로, 두 용도 모두에서'CFSTR() '을 사용하는 것이 좋습니다. –

+0

다음 번에 비워 둬서 고마워. :) – matti

답변

1
- (IBAction)buttonPressed:(id)sender 
{ 
    myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction) userInfo:nil repeats:YES]; 
} 

- (void)timerAction 
{ 

    if (myButton.userInteractionEnabled == YES) { 
     //do something 
    } 

    if ([myButton.tag == 0) { 
     [myTimer invalidate]; 
     [myButton setTag:2]; 
     myButton.userInteractionEnabled = YES; 
    }else{ 
     myButton.userInteractionEnabled = NO; 
     int currentTime = myButton.tag; 
     int newTime = currentTime - 1; 
     [myButton setTag:newTime]; 
    } 

} 

. 초기 태그 버튼을 "2"로 설정해야합니다.

+0

좋은 설명 !! –

+0

많은 감사합니다! 시도해 볼게!:) – matti

관련 문제