zoukankan      html  css  js  c++  java
  • 有用的代码

     

    1. 遍历NavigationController栈中的视图

    复制代码
    XXView *rootViewController = nil;
    for (UIViewController *VC in self.navigationController.viewControllers) {
    if ([VC isKindOfClass:[XXView class]]) {
    rootViewController = (XXView *)VC;
    }
    }
    [self.navigationController popToViewController:rootViewController animated:YES];
    复制代码

     2. 背景音乐播放,支持mp3格式
    需要先导入框架及代码中#import <AVFoundation/AVFoundation.h>

    复制代码
    NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"backgrounmusic" ofType:@"mp3"];
    NSURL *url = [[NSURL alloc] initFileURLWithPath:musicPath];

    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    // 创建播放器
    self.myBackMusic = player; //赋值给自己定义的类变量
    [url release];
    [player release];

    [myBackMusic prepareToPlay];
    [myBackMusic setVolume:1];
    myBackMusic.numberOfLoops = -1; //设置音乐播放次数 -1为一直循环
    if(mainMusicStatus)
    {
    [myBackMusic play]; //播放
    }
    复制代码

    3. 按钮播放声音、播放短声音

    复制代码
    需要导入框架#import <AudioToolbox/AudioToolbox.h>

    NSString *thesoundFilePath = [[NSBundle mainBundle] pathForResource:@"Clap Crowd" ofType:@"caf"]; //音乐文件路径
    CFURLRef thesoundURL = (CFURLRef)[NSURL fileURLWithPath:thesoundFilePath];
    AudioServesCreateSystemSoundID(thesoundURL, &sameViewSoundID);
    //变量SoundID与URL对应

    AudioServicesPlaySystemSound(sameViewSoundID); //播放SoundID声音
    复制代码

    4. 判断网络是否连接

    复制代码
    /***
    * 此函数用来判断是否网络连接服务器正常
    * 需要导入Reachability类
    */
    + (BOOL)isExistenceNetwork
    {
    BOOL isExistenceNetwork;
    Reachability *reachability = [Reachability reachabilityWithHostName:@""]; // 测试服务器状态

    switch([reachability currentReachabilityStatus]) {
    case NotReachable:
    isExistenceNetwork = FALSE;
    break;
    case ReachableViaWWAN:
    isExistenceNetwork = TRUE;
    break;
    case ReachableViaWiFi:
    isExistenceNetwork = TRUE;
    break;
    }
    return isExistenceNetwork;
    }
    复制代码

    5. 实时通知网络状况

    复制代码
    /*
    * 在应用委托的方法didFinishLaunchingWithOptions中添加
    */

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    reachability = [[Reachability reachabilityWithHostName:@"www.baidu.com"] retain];
    [reachability startNotifier];
    ........
    return YES;

    /**
    *此函数通过判断联网方式,通知给用户
    */
    - (void)reachabilityChanged:(NSNotification *)notification
    {
    Reachability *curReachability = [notification object];
    NSParameterAssert([curReachability isKindOfClass:[Reachability class]]);
    NetworkStatus curStatus = [curReachability currentReachabilityStatus];
    if(curStatus == NotReachable) {
    [DOIN_Util logFax:@"连接失败"];
    }
    }
    复制代码

    6. 延时函数和Timer的使用

    复制代码
    //延时函数:
    [NSThread sleepForTimeInterval:5.0]; //暂停5s.


    //Timer的使用:
    NSTimer *connectionTimer; //timer对象

    //实例化timer
    self.connectionTimer=[NSTimerscheduledTimerWithTimeInterval:1.5 target:selfselector:@selector(timerFired:) userInfo:nil repeats:NO];
    [[NSRunLoop currentRunLoop]addTimer:self.connectionTimer forMode:NSDefaultRunLoopMode];
    //用timer作为延时的一种方法
    do{
    [[NSRunLoopcurrentRunLoop]runUntilDate:[NSDatedateWithTimeIntervalSinceNow:1.0]];
    }while(!done);

    //timer调用函数
    -(void)timerFired:(NSTimer *)timer{
    done =YES;
    }
    复制代码
  • 相关阅读:
    背包系列 hdu3449 有依赖背包
    背包系列 hdu 3535 分组背包
    屏蔽scrollview的滚动
    高精度算法代码
    输入法出现时,中间固定,底部上移的代码
    排序之分治排序
    排序之双向冒泡排序
    Miller Rabin 大素数测试
    来聊聊WWDC 苹果大会上的那些黑科技
    不想成为好leader的程序猿不是好攻城狮
  • 原文地址:https://www.cnblogs.com/allanliu/p/4480027.html
Copyright © 2011-2022 走看看