iOS自定義Button點(diǎn)擊附帶音效

第三方微博客戶端Weico上面的每個(gè)按鈕點(diǎn)擊都有音效,今天來(lái)實(shí)現(xiàn)這個(gè)功能。

一、首先從weico的ipa包中獲取到音效文件,copy到自己的工程中來(lái)

二、添加AVFoundation.framework


三、自定義Button繼承自UIButton

WYButton.h:

WYButton.m:

#import"WYButton.h"

#import

@interfaceWYButton()

{

SystemSoundIDsoundFileObject;

}

@end

@implementationWYButton

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event

{

[selfplaySoundEffect:@"weico_click"type:@"wav"];

}

- (void)playSoundEffect:(NSString*)name type:(NSString*)type

{

//得到音效文件的地址

NSString*soundFilePath =[[NSBundlemainBundle]pathForResource:nameofType:type];

//將地址字符串轉(zhuǎn)換成url

NSURL*soundURL = [NSURLfileURLWithPath:soundFilePath];

//生成系統(tǒng)音效id

AudioServicesCreateSystemSoundID((__bridgeCFURLRef)soundURL, &soundFileObject);

//播放系統(tǒng)音效

AudioServicesPlaySystemSound(soundFileObject);

}

@end


四、新建這個(gè)自定義Button類的實(shí)例,點(diǎn)擊的時(shí)候就附帶聲音效果了


代碼不多,有興趣的朋友可以自己敲敲看下效果。


音效播放的代碼轉(zhuǎn)自:http://soohu.github.io/ios/2015/05/02/iOS%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E2%80%94%E2%80%94%E6%B7%BB%E5%8A%A0%E9%9F%B3%E6%95%88%E5%92%8C%E6%92%AD%E6%94%BE%E9%9F%B3%E4%B9%90/

更新:

上述自定義Button的- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event方法中,在播放音效的代碼后要加上如下代碼,否則實(shí)例button點(diǎn)擊后,你所寫(xiě)的點(diǎn)擊效果將失效

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event

{

[selfplaySound:@"weico_click"type:@"wav"];

[supertouchesBegan:toucheswithEvent:event];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容