IOS的一些小技巧

1.播放短聲音   

 

    SystemSoundID shake_sound_male_id = 0;
    NSString *thesoundFilePath = [[NSBundle mainBundle] pathForResource:@"ReceivedMessage" ofType:@"caf"]; //音樂文件路徑
    CFURLRef thesoundURL = (CFURLRef)CFBridgingRetain([NSURL fileURLWithPath:thesoundFilePath]);
    AudioServicesCreateSystemSoundID(thesoundURL, &shake_sound_male_id);
    AudioServicesPlaySystemSound(shake_sound_male_id);
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//播放震動

2.播放系統聲音

 

    AudioServicesPlaySystemSound(1004); //更換播放系統的聲音 id值能夠百度查詢
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//震動

3.在主線程中刷新UI

 

       dispatch_async(dispatch_get_main_queue(), ^{
           _lbName=@"Name";
        });

4.函數延時執行

 

//這樣實現延時調用是錯誤的,playSound是不會被調用的 能夠在主線程調用
dispatch_async(dispatch_get_global_queue(2, 0), ^{
        [self performSelector:@selector(playSound) withObject:nil afterDelay:0.5f];//NSTimer 也須要注意這點
 });

5.一個函數短期屢次觸發,但願函數裏摸個代碼段只執行一次

 

//這個是抄的 原地址http://www.cocoachina.com/bbs/read.php?tid=193276
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(playSound) object:nil];
[self performSelector:@selector(playSound) withObject:nil afterDelay:0.5f];

 

    之後要多記錄一下咯,很短覺得能記住,過段時間又忘了,又要打開之前的項目來看,本身記憶力真的不行,之後就多php

記錄下。async

相關文章
相關標籤/搜索