zoukankan      html  css  js  c++  java
  • 笔记隐藏状态栏,播放音乐,获取文件路径,nsthread,文件文件夹操作,plist 时间

    iphone 学习笔记

    1。隐藏状态栏[[UIApplication sharedApplicationsetStatusBarHidden:YES];

    /******************************************************************************
    1、取随机数:
    NSData *datanow = [NSData data];       
    int i = (int)datanow;               
    srand(i);                              
    rand();
    //int effectPicNum = rand()%7;
    ******************************************************************************/
    /******************************************************************************
    2、播放音乐:
    -(void) playMusic
    {
    @try{
    //取文件路径
    NSString *musicFilePath = [[NSBundle mainBundlepathForResource:@"startLogo" ofType:@"mp3"];       
    NSURL *musicURL = [[NSURL allocinitFileURLWithPath:musicFilePath];  
    musicPlayer= [[AVAudioPlayerallocinitWithContentsOfURL:musicURL error:nil];
    [musicURL release];
    //[musicPlayer prepareToPlay];
    //[musicPlayer setVolume:1];            //设置音量大小
    musicPlayer.numberOfLoops0//设置播放次数,-1为一直循环,0为一次
    [musicPlayerplay]; 
    }
    @catch(NSException* e) {
    }

    ******************************************************************************/
    /******************************************************************************
    3、每隔0.8秒执行timeCount方法
    NSTimer*countTimer;
    countTimer= [NSTimerscheduledTimerWithTimeInterval0.8targetselfselector@selector(timeCount:)  userInfonilrepeatsYES];   
    [countTimerfire];     //执行timer
    ******************************************************************************/
    /******************************************************************************
    4、延迟1秒执行test方法:
    [selfperformSelector:@selector(testwithObject:nilafterDelay:0.1];
    ******************************************************************************/
    /******************************************************************************
    5、启动线程:
    [NSThreaddetachNewThreadSelector:@selector(transImagetoTarget:selfwithObject:nil]; 
    timer=[NSTimerscheduledTimerWithTimeInterval:0.03target:selfselector:@selector(TimerClock:) userInfo:nilrepeats:YES]; //启动一个NSTimer执行广播
    [timerfire];  //执行timer

    -(void)TimerClock:(id)sender
    {
    //控制延迟触发
    if(Timecontrol>1) {   
    [timerConditionbroadcast];      //广播,触发处于等待状态的timerCondition

    }

    -(void)transImage

    isRunning=YES;
    while (countTime < COUNTTIME) {
    [timerConditionwait];
    lim += 255 / (2 * KFrame);
    [selfprocessImage];
    countTime += 1000 / KFrame;
    }
    [timerinvalidate];
    isRunning=NO;
    }
    ******************************************************************************/
    /******************************************************************************
    6、获取文件路径:
    //通过NSHomeDirectory获得文件路径
    NSString *homeDirectory = NSHomeDirectory();
    NSString *fileDirectory = [homeDirectory stringByAppendingPathComponent:@"temp/app_data.plist"];

    //使用NSSearchPathForDirectoriesInDomains检索指定路径
    NSArray*path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMaskYES);
    //常量NSDocumentDirectory表示正在查找Documents目录的路径(使用NSCachesDirectory表明要查找的时Caches文件夹),常量NSUserDomainMask表明我们希望将搜索限制于我们应用程序的沙盒,最后一个参数决定了是否“展开”波浪线符号。
    //在Mac系统中,‘~’表示主路经(Home),如果不展开,路径看起来就是:‘~/Documents’,展开后即得到完整路径。这个参数一直设置位真即可。
    NSString *documentsDirectory = [paths objectAtIndex:0];z
    NSString *fileDirectory = [documentsDirectory stringByAppendingPathComponent:@"file.txt"];

    //使用Foundation中的NSTemporaryDirectory函数直接返回代表temp文件夹的全路径的字符串对象
    NSString *tempDirectory = NSTemporaryDirectory();
    NSString*file = [tempDirectory stringByAppendingPathComponent:@"file.txt"];


      Example:
    NSArray*path = NSSearchPathForDirectoriesInDomains(NSCachesDirectoryNSUserDomainMaskYES);
    NSString *docDir = [path objectAtIndex:0];
    NSLog(@"filepath:%@",docDir);
    NSString*str = @"hello.jpg";
    NSString*filepath = [docDir stringByAppendingPathComponent:str];
    //NSString *filepath = [docDir stringByAppendingPathComponent:[NSString stringWithUTF8String:"///mest.txt"]];
    NSLog(@"filepath:%@",filepath);
    BOOLsuccess = [[NSFileManagerdefaultManager]createFileAtPath: filepath contents:nilattributes:nil];
    NSLog(@"result",success);
    printf("Create File:%s %s.",[filepath UTF8String], success ? "Success""Error");

    NSString* reValue= [NSString stringWithString:@"\"success\""];
    NSLog(reValue);
    ******************************************************************************/
    /************************************************************************************************************************************************************
    7文件、文件夹操作
    //如果"/Documents/Theme"路径不存在,则创建。
    if(![[NSFileManagerdefaultManager]fileExistsAtPath:themePath])
    {
    [[NSFileManagerdefaultManagercreateDirectoryAtPath:themePath attributes:nil];
    }
    //删除已存在的同名文件夹
    if([[NSFileManagerdefaultManagerfileExistsAtPath:savePath]) {
    [[NSFileManagerdefaultManagerremoveItemAtPath:savePath error:NULL];
    }
    ************************************************************************************************************************************************************/
    /************************************************************************************************************************************************************
    7 子线程抛给主线程:
    [selfperformSelectorOnMainThread:@selector(shiftViewwithObject:nilwaitUntilDone:YES];

    ************************************************************************************************************************************************************/
    /************************************************************************************************************************************************************
    8获取当前时间
    NSDateFormatter*formatter = [[NSDateFormatterallocinit];
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSString *locationString=[formatter stringFromDate: [NSDate date]];

    //获取当前时间作为productId
    NSDateFormatter*formatter = [[NSDateFormatterallocinit];
    [formatter setDateFormat:@"hhmmss"];
    NSString *locationString=[formatter stringFromDate: [NSDate date]];
    downloadInfo.productId = locationString;
    [formatter release];
    /******************************************************************************
     函数名称  : getDate
    函数描述  : 获取当前日期时间
     输入参数  : N/A
     输出参数  : N/A
     返回值    : NSString 当前时间
     备注     :
     ******************************************************************************/
    -(NSString *)getDate
    {
    NSDateFormatter*formatter = [[NSDateFormatterallocinit];
    [formatter setDateFormat:@"yyyy-MM-dd EEEE HH:mm:ss a"];
    NSString *locationString=[formatter stringFromDate: [NSDate date]];
    [formatter release];
    return locationString;
    }
    大写的H日期格式将默认为24小时制,小写的h日期格式将默认为12小时
    不需要特别设置,只需要在dataFormat里设置类似"yyyy-MMM-dd"这样的格式就可以了
    日期格式如下:
    y  年  Year  1996; 96  
    M  年中的月份  Month  July; Jul; 07  
    w  年中的周数  Number  27  
    W  月份中的周数  Number  2  
    D  年中的天数  Number  189  
    d  月份中的天数  Number  10  
    F  月份中的星期  Number  2  
    E  星期中的天数  Text  Tuesday; Tue  
    a  Am/pm 标记  Text  PM  
    H  一天中的小时数(0-23)  Number  0  
    k  一天中的小时数(1-24)  Number  24  
    K  am/pm 中的小时数(0-11)  Number  0  
    h  am/pm 中的小时数(1-12)  Number  12  
    m  小时中的分钟数  Number  30  
    s  分钟中的秒数  Number  55  
    S  毫秒数  Number  978  
    z  时区  General time zone  Pacific Standard Time; PST; GMT-08:00  
    Z  时区  RFC 822 time zone  -0800
    ************************************************************************************************************************************************************/
    /************************************************************************************************************************************************************
    读取和写入plist文件

    plist文件是标准的xml文件,在cocoa中可以很简单地使用。这里介绍一下使用方法: 以下代码在Mac和iPhone中均适用。 
    写入plist文件: NSMutableDictionary * dict = [ [ NSMutableDictionary alloc ] initWith 
      
    plist文件是标准的xml文件,在cocoa中可以很简单地使用。这里介绍一下使用方法:    
       
    以下代码在Mac和iPhone中均适用。
       
    写入plist文件:  
    NSMutableDictionary* dict = [ [ NSMutableDictionaryalloc ] initWithContentsOfFile:@"/Sample.plist"];
    [ dict setObject:@"Yes"forKey:@"RestartSpringBoard"];
    [ dict writeToFile:@"/Sample.plist"atomically:YES];
       
    读取plist文件:
       
    NSMutableDictionary* dict =  [ [ NSMutableDictionaryalloc ] initWithContentsOfFile:@"/Sample.plist"];
    NSString* object = [ dict objectForKey:@"RestartSpringBoard" ];
    ************************************************************************************************************************************************************/

  • 相关阅读:
    删除旧版vue-cli
    vue.extend和vue.component的区别
    vue-loader处理vue文件
    CentOS7安装iptables防火墙
    java类的初始化顺序
    java的接口和抽象类的理解
    js中的in操作符
    js中call和apply函数
    js的类型转换
    闭包作用
  • 原文地址:https://www.cnblogs.com/Cristen/p/2849597.html
Copyright © 2011-2022 走看看