iOS学习笔记 (5)NSDate和NSTimer

NSDate类的使用


 //1、创建日期对象

        NSDate *date1=[[NSDate alloc]init];

        NSLog(@"%@",date1);//默认格林尼治时间相差8个时区

        //2

        NSDate  *date2=[NSDate date];

        //返回一个从1970-01-01 000000 后一段时间的时间

        date2=[NSDate dateWithTimeIntervalSince1970:10];

        //返回当前时间一段时间后的时间

        NSDate *date3=[NSDate dateWithTimeIntervalSinceNow:3600*8];

        NSLog(@"%@",date2);

        //相对于某个日期后的什么时间

        NSDate *date4=[NSDate dateWithTimeInterval:3600 sinceDate:date2];

        NSLog(@"%@",date4);

        

        //返回两个时间点较早的时间

        

        [date4 earlierDate:date3];

        //返回两个时间点比较晚的时间

        [date3 laterDate:date2];

        //比较两个时间大小

        [date3 compare:date2];

        //判断两个时间是否相等

        [date3 isEqualToDate:date2];

        

        //返回一个很久很久的时间

       NSDate *date6= [NSDate distantPast];

        NSLog(@"%@",date6);

        

        //返回一个未来的时间

        NSDate *date7=[NSDate distantFuture];

       // NSLog(@"%@",date7);

        

        //将一个定时器(NSTimer)的开启时间设置为很早以前的时间会启动定时器

        //设置成为一个未来的时间的一个时间,会关闭定时器

        

        NSTimeInterval sub=[date1 timeIntervalSinceDate:date2];

        NSLog(@"%f",sub);

        //计算当前时间与系统时间的差值

        NSTimeInterval sub2=[date6 timeIntervalSinceNow];

        NSLog(@"%.2f",sub2);


NSTimer类的使用




@implementation RootViewController

{

    NSTimer *timer;//定时器

}

-(void)dealloc

{

    //释放timer

    if(!timer)

    {

        [timer invalidate];

    }

    [super dealloc];

}

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    [self createButton];

   

    [self createLable];

    [self createTimer];

}



#pragma mark-- 创建button

-(void)createButton

{

    UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button setFrame:CGRectMake(100, 50, 100, 50)];

    button.backgroundColor=[UIColor redColor];

    [button setTitle:@"开始" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchDown];

    [self.view addSubview:button];

    [button release];

    

}


-(void)onClick:(UIButton *)button

{

    static BOOL isRuning=NO;

    if(isRuning){

        //启动计时器,将时间设置为过去的一个时间

        [timer setFireDate:[NSDate distantPast]];

        [button setTitle:@"停止" forState:UIControlStateNormal];

        isRuning=NO;

    }else{

        //暂停定时器,将时间设置为未来的一个时间

        [timer setFireDate:[NSDate distantFuture]];

        [button setTitle:@"开始" forState:UIControlStateNormal];

        isRuning=YES;

    }

}

#pragma mark 创建timer

-(void)createTimer

{

    //第一个参数是timer得间隔时间,就是说我的timer多少时间启动一次

    //第二个参数是接受消息的的对象

    //第三个参数是方法选择器。就是多少时间调用一次该方法

    //第四个参数是用户信息,比如error

    //第五个参数是否重复执行

    //定时器是开始即启动

    

    

    timer=[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timeRun) userInfo:nil repeats:YES];

    

    //先暂停,将时间设置为未来的一个时间

    [timer setFireDate:[NSDate distantFuture]];

  }


-(void)timeRun

{

    static int count=1;

    NSLog(@"定时器第%d次调用!",count);

    count++;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值