gpt4 book ai didi

ios - 按下图像按钮时添加声音

转载 作者:行者123 更新时间:2023-11-29 04:06:10 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com