2014-11-14 59 views
1

我一直堅持這一段時間。所以在我的應用程序中,我會有播放聲音的按鈕。當用戶單擊按鈕(button1.png)時,我想將圖像更改爲(button2.png),然後當聲音播放完畢時,我想將圖片圖片更改爲原始圖片。我認爲回調將是最好的設置,但即時遇到麻煩。幫助將被讚賞。如何設置回調函數?

這裏是我的代碼

#import "ViewController.h" 
#import <AudioToolbox/AudioToolbox.h> 

@interface ViewController() 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
[scrollView setScrollEnabled:YES]; 
// change setContentSize When making scroll view Bigger and adding more items 
[scrollView setContentSize:CGSizeMake(320, 1000)]; 

} 
- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

#pragma mark - CallBackMethods 









#pragma mark - SystemSoundIDs 
SystemSoundID sound1; 







#pragma mark - Sound Methods 
-(void)playSound1 
{ 
NSString* path = [[NSBundle mainBundle] 
        pathForResource:@"Sound1" ofType:@"wav"]; 
NSURL* url = [NSURL fileURLWithPath:path]; 
AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &sound1); 


static void (^callBAck)(SystemSoundID ssID, void *something); 

callBAck = ^(SystemSoundID ssID, void *something){ 
    [button1 setImage:@"WhiteButton.png" forState:UIControlStateNormal]; 
}; 

AudioServicesAddSystemSoundCompletion(sound1, 
             NULL, 
             NULL, 
             callback, 
             NULL); 

AudioServicesPlaySystemSound(sound1); 
} 
- (IBAction)button:(id)sender { 
NSLog(@"Hello"); 
[button1 setImage:[UIImage imageNamed:@"ButtonPressed.png"] forState:UIControlStateNormal]; 
[self playSound1];  
} 
@end 

回答

0

AudioToolboxÇ框架(注意Ç風格的函數調用)。 所以你通過它的回調一定是C function pointer

望着AudioServicesSystemSoundCompletionProc類型,你需要通過爲AudioServicesAddSystemSoundCompletion呼叫的第四個參數回:

typedef void (*AudioServicesSystemSoundCompletionProc) (SystemSoundID ssID, void *clientData);

它會告訴你,你需要聲明一個C函數接受兩個參數並返回void作爲回調處理程序並將其傳遞到AudioServicesAddSystemSoundCompletion

// Declare this anywhere in the source file. 
// I would put this before @implement of the class. 
void audioCompletionHandler(SystemSoundID ssID, void *clientData) { 
    NSLog(@"Complete"); 
} 

... 

- (void)playSound { 
    ... 
    // To pass the function pointer, add & before the function name. 
    AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, &audioCompletionHandler, NULL); 
    AudioServicesPlaySystemSound(sound); 
} 
相關問題