您的位置:首页 > 移动开发 > IOS开发

iOS学习笔记 (5)NSDate和NSTimer

2015-07-02 20:29 411 查看
NSDate类的使用

 //1、创建日期对象
       
NSDate *date1=[[NSDate
alloc]init];
       
NSLog(@"%@",date1);//默认格林尼治时间相差8个时区
       
//2、
       
NSDate  *date2=[NSDate
date];

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

        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++;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: