Ios 如何设置回调函数?

Ios 如何设置回调函数?,ios,callback,ios8,xcode6,Ios,Callback,Ios8,Xcode6,我已经在这上面呆了一段时间了。所以在我的应用程序中,我会有播放声音的按钮。当用户单击按钮(button1.png)时,我想将图像更改为(button2.png),然后当声音播放完毕时,我想将图片图片更改为原始图像。我想回拨是最好的设置,但我有麻烦。我们将不胜感激 这是我的密码 #import "ViewController.h" #import <AudioToolbox/AudioToolbox.h> @interface ViewController () @end @imp

我已经在这上面呆了一段时间了。所以在我的应用程序中,我会有播放声音的按钮。当用户单击按钮(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
#导入“ViewController.h”
#进口
@界面视图控制器()
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
[滚动视图设置可滚动:是];
//在使滚动视图变大并添加更多项目时更改setContentSize
[scrollView setContentSize:CGSizeMake(3201000)];
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
#pragma标记-回调方法
#pragma标记-系统声音ID
SystemSoundID sound1;
#pragma标记-声音方法
-(无效)播放声音1
{
NSString*路径=[[NSBundle mainBundle]
pathForResource:@“Sound1”,其类型为@“wav”];
NSURL*url=[NSURL fileURLWithPath:path];
AudioServicesCreateSystemSoundID((uu桥CFURLRef)url和sound1);
静态无效(^callBAck)(SystemSoundID ssID,void*something);
回调=^(SystemSoundID ssID,void*something){
[按钮1设置图像:@“WhiteButton.png”用于状态:UIControlStateNormal];
};
AudioServicesAndSystemSoundCompletion(声音1,
无效的
无效的
回拨,
无效);
AudioServicesPlaySystemSound(声音1);
}
-(iAction)按钮:(id)发送者{
NSLog(@“你好”);
[button1 setImage:[UIImage ImageName:@“ButtonPressed.png”]用于状态:UIControlStateNormal];
[自动播放声音1];
}
@结束

AudioToolbox是一个C框架(请注意C风格的函数调用)。 因此,传递给它的回调必须是

查看需要作为回拨的
AudioServicesAndSystemSoundCompletion
的第四个参数传入的类型:

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