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