NSTimer的使用

- (void)addTimer

{

self.timer =
[NSTimerscheduledTimerWithTimeInterval:2.0target:selfselector:@selector(nextImage)
userInfo:nilrepeats:YES];

[[NSRunLoopcurrentRunLoop]
addTimer:self.timerforMode:NSRunLoopCommonModes];

}

//销毁过程

- (void)removeTimer

{

[self.timer invalidate];

self.timer = nil;

}

当然,上面这段代码,有可能会在主线程中执行,这样会导致线程阻塞。实际使用的话,还是应该开辟新线程

NSTimer的使用,布布扣,bubuko.com

时间: 2024-08-01 20:12:43

NSTimer的使用的相关文章

NSTimer

NSTimer叫做“定时器”,它的作用如下在指定的时间执行指定的任务每隔一段时间执行指定的任务 调用下面的方法就会开启一个定时任务+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;每隔ti秒,调用一次aTarget的aSelector方法,yesOr

IOS开发—NSTimer

创建timer对象的三种方法 一.这两个类方法创建一个timer并把它指定到一个默认的runloop模式中 + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo; + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(i

iOS:三种常见计时器(NSTimer、CADisplayLink、dispatch_source_t)的使用

一.介绍 在iOS中,计时器是比较常用的,用于统计累加数据或者倒计时等,例如手机号获取验证码.计时器大概有那么三种,分别是:NSTimer.CADisplayLink.dispatch_source_t 二.使用 @property (strong,nonatomic)NSTimer *timer; @property (strong,nonatomic)CADisplayLink *displaylinkTimer; @property (strong,nonatomic)dispatch_s

NSRunLoop && NSTimer

新的一年的开始,希望大家一切越来越好,越来越开心快乐!!! 定时器及运行循环 NSRunLoop是iOS消息机制的处理模式 NSRunLoop的主要作用:控制NSRunLoop里面线程的执行和休眠,在有事情做的时候使当前NSRunLoop控制的线程工作,没有事情做让当前NSRunLoop的控制的线程休眠. 通过所有的“消息”都被添加到了NSRunLoop中去,而在这里这些消息并分为“input source”和“Timer source” 并在循环中检查是不是有事件需要发生,如果需要那么就调用相

NSTimer类的使用

创建一个 Timer + scheduledTimerWithTimeInterval: invocation: repeats: + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti   invocation:(NSInvocation *)invocation   repeats:(BOOL)yesOrNo; + scheduledTimerWithTimeInterval: target: selector: user

iOS开发——实用技术OC篇&NSTimer使用注意点及总结

NSTimer使用注意点及总结 总结以下在NSTimer的使用中遇到的一些问题: 1. 不要在dealloc函数中停止并释放NSTimer 如果这样做,会导致对象永远无法调用dealloc函数,也就是会造成内存泄漏. 一个比较合理的解释是NSTimer的回调方法具有retain属性,所以不停止它的情况下被引用对象的retainCount无法降为0,导致内存泄漏的死循环 2.因为要实现类似视频软件里面,UIScrollview定时循环滑动,用到了NSTimer类.在特定时事件情况下需要暂停,和重新

iOS容易造成循环引用的三种场景NSTimer以及对应的使用方法(一)

NSTimer A timer provides a way to perform a delayed action or a periodic action. The timer waits until a certain time interval has elapsed and then fires, sending a specified message to a specified object(timer就是一个能在从现在开始的未来的某一个时刻又或者周期性的执行我们指定的方法的对象)

替代NSTimer的block计时器!! !

既然NSTimer容易造成内存泄露,那就给初出茅庐的小程序员们带来一个惊喜吧! 话不多说上代码: LIST -(void)startTime{ timeout_int=0; dispatch_queue_t queue_global =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYP

项目问题总结:Block内存泄露 以及NSTimer使用问题

BLock的内存泄露 在我们代码中关于block的使用可以说随处可见,第一次接触block的时候是关于UIView的块动画,那时觉得block的使用好神奇,再后来分析总结为block其实就是一个c语言函数,只是我们可以在任意处调用此函数.有了这样的理解我开始经常使用block.在做项目以后发现使用block竟然会引起内存泄露,于是开始自己调试研究block的内存管理问题. 普通的block使用(包括块动画) 这里有一个简单的block使用,在里面我们可以添加任何自己想进行的操作,大部分的使用也是

iOS开发总结(A0) - NStimer

NStimer是ios开发的计时器,简单易用,但有几个注意事项 1. 创建NStimer的两个常用方法是 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo; + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInte