注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

ksnowlv的博客

积土成丘,集腋成裘,坚持再坚持!

 
 
 

日志

 
 

时间差类型的计时  

2013-10-28 18:15:43|  分类: IOS_其它 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

CFAbsoluteTimeGetCurrent

Returns the current system absolute time.

CFAbsoluteTime CFAbsoluteTimeGetCurrent ();
Return Value

The current absolute time.

Discussion

Absolute time is measured in seconds relative to the absolute reference date of Jan 1 2001 00:00:00 GMT. A positive value represents a date after the reference date, a negative value represents a date before it. For example, the absolute time -32940326 is equivalent to December 16th, 1999 at 17:54:34. Repeated calls to this function do not guarantee monotonically increasing results. The system time may decrease due to synchronization with external time references or due to an explicit user change of the clock.

Availability
  • Available in iOS 2.0 and later.

何为时间差类型的计时?应用在什么场景?为什么需要这种类型的计时方法?见如下CODE:

//

//  CFAbsoluteTimeGetCurrentTestViewController.m

//   do something   CFAbsoluteTimeGetCurrentTest

//

//  Created by lv wei on 13-10-28.

//  Copyright (c) 2013 lv wei. All rights reserved.

//


#import "CFAbsoluteTimeGetCurrentTestViewController.h"


static const NSInteger KMaxTimeValue = 100;


@interface CFAbsoluteTimeGetCurrentTestViewController ()

{

    CFAbsoluteTime _curTime;

    NSTimer* _timer;

    NSInteger _countDown;

}


@property(nonatomic, assign) BOOL isCountDown;


@end


@implementation CFAbsoluteTimeGetCurrentTestViewController

@synthesize isCountDown = _isCountDown;


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    

    [self setIsCountDown:YES];

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (void)dealloc

{

    if (_timer) {

        

        if (_timer.isValid) {

            [_timer invalidate];

        }

        

        _timer = nil;

    }

}


- (void)setIsCountDown:(BOOL)isCountDown

{

    if (_isCountDown == isCountDown) {

        return;

    }

    

    _isCountDown = isCountDown;

    

    if (_isCountDown) {

        

        if ( nil == _timer ) {

            _timer = [NSTimer scheduledTimerWithTimeInterval:1/10.0 target:self selector:@selector(updateCountDownTimeEvent) userInfo:nil repeats:YES];

            _curTime = CFAbsoluteTimeGetCurrent();

            _countDown = KMaxTimeValue;

            NSLog(@"coutDown = %d",_countDown);

        }

    }

    else

    {

        if (_timer) {

            

            if (_timer.isValid) {

                [_timer invalidate];

            }

            

            _timer = nil;

            _countDown = 0;

            _curTime = CFAbsoluteTimeGetCurrent();

        }

        

    }

}


- (void)updateCountDownTimeEvent

{

    CFAbsoluteTime curTime = CFAbsoluteTimeGetCurrent();

    NSInteger countDown = _countDown;

    _countDown = KMaxTimeValue - (curTime - _curTime);

    

    NSLog(@"%d",_countDown);

    

    if (countDown != _countDown) {

        //处理整秒事件

    }

    //为什么采用这种倒计时方式而没有采用整秒那种形式的倒计时呢?

    //如果程序锁屏或进入后台时,计时器会停止,进来时依然按原来时间计时。

    //这种计时按时间差来计时,和计时器的间隔基本上没有关系

    //为啥没有采用整秒呢?原因希望从后台或非活动状态进入到活动状态能快速响应变化

    //给人一种时间一直在走的假象。有时,转换思路,会有更好的处理方案。

}



@end


日志如下:

2013-10-28 18:03:49.730  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] Cannot find executable for CFBundle 0xb06b460 </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.0.sdk/System/Library/AccessibilityBundles/CertUIFramework.axbundle> (not loaded)

2013-10-28 18:03:49.881  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] coutDown = 100

2013-10-28 18:03:49.982  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 99

2013-10-28 18:03:50.082  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 99

2013-10-28 18:03:50.182  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 99

2013-10-28 18:03:50.281  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 99

2013-10-28 18:03:50.382  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 99

2013-10-28 18:03:50.482  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 99

2013-10-28 18:03:50.582  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 99

2013-10-28 18:03:50.682  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 99

2013-10-28 18:03:50.781  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 99

2013-10-28 18:03:50.882  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 98

2013-10-28 18:03:50.982  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 98

2013-10-28 18:03:51.082  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 98

2013-10-28 18:03:51.181  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 98

2013-10-28 18:03:51.282  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 98

2013-10-28 18:03:51.382  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 98

2013-10-28 18:03:51.482  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 98

2013-10-28 18:03:51.582  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 98

2013-10-28 18:03:51.682  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 98

2013-10-28 18:03:51.781  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 98

2013-10-28 18:03:51.882  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 97

2013-10-28 18:03:51.982  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 97

2013-10-28 18:03:52.082  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 97

2013-10-28 18:03:52.182  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 97

2013-10-28 18:03:52.282  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 97

2013-10-28 18:03:52.382  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 97

2013-10-28 18:03:52.482  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 97

2013-10-28 18:03:52.582  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 97

2013-10-28 18:03:52.682  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 97

2013-10-28 18:03:52.782  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 97

2013-10-28 18:03:52.882  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 96

2013-10-28 18:03:52.982  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 96

2013-10-28 18:03:53.082  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 96

2013-10-28 18:03:53.182  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 96

2013-10-28 18:03:53.282  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 96

2013-10-28 18:03:53.382  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 96

2013-10-28 18:03:53.482  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 96

2013-10-28 18:03:53.582  do something   CFAbsoluteTimeGetCurrentTest[6014:70b] 96








  评论这张
 
阅读(257)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018