首页 > 移动平台 > 详细

iOS中的定时器

时间:2015-02-07 11:25:10      阅读:265      评论:0      收藏:0      [点我收藏+]

iOS中的两个定时器:

1.NSTimer  ------>简单使用,时间多于1秒使用
2.CADisplayLink  ------>简单使用,时间小于一秒使用,每秒调用60次
 

@property(nonatomic,strong)NSTimer* timer;

1.1手动加入消息循环

 

// 开启定时器

 

-(void)startTimer{

    self.timer=[NSTimer timerWithTimeInterval:3 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];

    

    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

    

}

 

// 移除定时器

 

- (void)stopTimer

{

    [self.timer invalidate];

    self.timer = nil;

}

——————————————————————————————————————————————————————————————————————————————————

1.2 自动加入消息循环

 

// 开启定时器

 

-(void)startTimer

{

         self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];

   

}

 

 // 移除定时器

 

- (void)stopTimer

{

    [self.timer invalidate];

     self.timer = nil;

}

——————————————————————————————————————————————————————————————————————————————————

2.

@property(nonatomic,strong)CADisplayLink* link;

 

 //开启定时器

 

-(void)startTimer

{

       self.link = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateLrc)];

    [self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];

}

//移除定时器

- (void)stopTimer

{

    [self.link  invalidate];

    self.link  = nil;

}

 

iOS中的定时器

原文:http://www.cnblogs.com/lijianyi/p/4278455.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!