2014-11-14 70 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); 
} 
相关问题