• IOS复习NStimer


    这是一个简单的计时器,每隔一秒执行一次
    网上有一篇博客叫你真的会nstimer了吗,可以搜索看看,写的很好
    - (void)viewDidLoad
    {
        [superviewDidLoad];
        NSTimer *timer=[NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(handleTimer:) userInfo:nilrepeats:YES];
        //如果是NO则timer在触发了回调函数调用完成之后会释放timer,如果是YES,则会重复调用函数
    }
    - (void) handleTimer: (NSTimer *) timer // timer的回调函数
    {
        //在这里进行处理
        NSLog(@"1");//上面的1是1秒
    //    for (int i=0; i<=1000000000;) {
    //        i++;
    //    }
    }
                      2013年8月7日,12:31,东南大学无锡分校桃园3宿舍106室
     

    接着写一个时钟吧,闹钟神马的就算了,

    想了一下,时钟首先获取当前事件,然后每隔一秒刷新一次,这个可以精确到毫秒,但是时钟没必要毫秒,浪费内存。

    下面是实现代码

    - (void)viewDidLoad
    {
        [superviewDidLoad];
        NSTimer *timer=[NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(handleTimer:) userInfo:nilrepeats:YES];
        //如果是NO则timer在触发了回调函数调用完成之后会释放timer,如果是YES,则会重复调用函数
    }
    #pragma mark -获得系统时间
    -(void)one
    {
        //获得系统时间
        NSDate * senddate=[NSDate date];
        NSDateFormatter *dateformatter=[[NSDateFormatteralloc] init];
        [dateformatter setDateFormat:@"HH:mm:ss"];
        NSString *locationString=[dateformatter stringFromDate:senddate];
        //[dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
        //NSString * morelocationString=[dateformatter stringFromDate:senddate];
        NSLog(@"%@",locationString);
        
        UILabel *sjLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 90)];
        [sjLabel setText:locationString];
        [self.view addSubview:sjLabel];
    }
    - (void) handleTimer: (NSTimer *) timer // timer的回调函数
    {
        //在这里进行处理
        [self one];
    }
    运行图就不写了,明显感觉出来这几篇博客写的不是太认真了,可能这是复习吧,没什么心情,或其他的,下一篇博客一定要认真写了。

  • 相关阅读:
    linux下文件/目录的默认权限
    linux中如何查看文件/文件夹的大小
    linux定时/计划任务
    shell脚本中EOF的妙用
    linux中的计算器
    linux操作系统的时间
    Anaconda如何设置网络代理
    前端学习问题积累
    angular-ng-zorro,自定义模态窗拖动指令
    angular学习笔记之父子传值
  • 原文地址:https://www.cnblogs.com/ioschen/p/3248859.html
Copyright © 2020-2023  润新知