首页 > 代码库 > (5.20)时间戳记触发定时器控制计数
(5.20)时间戳记触发定时器控制计数
// mainViewController.m
// 雪花
//
// Created by pg on 14-5-20.
// Copyright (c) 2014年 mqd. All rights reserved.
//
#import "mainViewController.h"
@interface mainViewController ()
//游戏时钟
@property(strong,nonatomic)CADisplayLink *gameTime;
//时钟戳记
@property(assign,nonatomic) CFTimeInterval startTime;
@end
@implementation mainViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//初始化时间撮箕
self.startTime = 0;
//1,实例化游戏时钟
self.gameTime = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateTime:)];
//2,添加到主运行循环
[self.gameTime addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
-(void)updateTime:(CADisplayLink *)sender{
if (self.startTime == 0) {
self.startTime = sender.timestamp;
}
CFTimeInterval deleTime = sender.timestamp - self.startTime;
if (deleTime > 1.0) {
NSLog(@"触发了");
self.startTime = sender.timestamp;
}
}
@end