我在scrollView中有此按钮,按下时需要播放声音。

UIButton *profileButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [profileButton setImage:[UIImage imageNamed:@"test.jpg"] forState:UIControlStateNormal];
    [profileButton setImage:[UIImage imageNamed:@"test.jpg"] forState:UIControlStateHighlighted];
    profileButton.frame = CGRectMake(0, 620, 320, 250);

[self.scrollView addSubview:profileButton];

我该怎么办?
谢谢,

最佳答案

在按钮上添加触摸

[profileButton addTarget:self action:@selector(onButtonPress) forControlEvents:UIControlEventTouchUpInside];

//需要这个包含文件和AudioToolbox框架
#import <AudioToolbox/AudioToolbox.h>

播放声音
-(void) onButtonPress {
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"mp3"];
    SystemSoundID soundID;
    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
    AudioServicesPlaySystemSound (soundID);
}

关于ios - 按下图像按钮时添加声音,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15186417/

10-09 02:24