NSTimer 用法小结

Timers的替代方法

如果只是要延迟消息的发送,可以使用NSObject的方法

- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait

+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget

创建Timer的三种方法

  1. scheduling a timer with the current run loop 
  2. creating a timer that you later register with a run loop
  3. initializing a timer with a given fire date

Scheduled Timers

以下两个方法自动注册新创建的timer到当前NSRunLoop对象,NSRunLoop的模式为默认的NSDefaultRunLoopMode

  • + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds invocation:(NSInvocation *)invocation repeats:(BOOL)repeats
  • + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
1 - (IBAction)startTimer:sender {  
2    
3     [NSTimer scheduledTimerWithTimeInterval:2.0  
4              target:self  
5              selector:@selector(targetMethod:)  
6              userInfo:[self userInfo]  
7              repeats:NO];  //需要重复调用目标方法时,repeats设置为YES
8 }

注:创建重复发送消息的timer一般需要保存一个引用,因为需要在某个时刻停止发送消息  [timer invalidate];

Unscheduled Timers

创建未注册的timer,使用时调用addTimer:forMode注册到NSRunLoop对象。

  • timerWithTimeInterval:target:selector:userInfo:repeats:
  • timerWithTimeInterval:invocation:repeats:
 1 - (IBAction)createUnregisteredTimer:sender {  
 2    
 3     NSMethodSignature *methodSignature = [self methodSignatureForSelector:@selector(invocationMethod:)];  
 4     NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];  
 5     [invocation setTarget:self];  
 6     [invocation setSelector:@selector(invocationMethod:)];  
 7     NSDate *startDate = [NSDate date];  
 8     [invocation setArgument:&startDate atIndex:2];  
 9    
10     NSTimer *timer = [NSTimer timerWithTimeInterval:0.5 invocation:invocation repeats:YES];  
11     self.unregisteredTimer = timer;  
12 }  
1 - (IBAction)startUnregisteredTimer:sender {  
2     if (unregisteredTimer != nil) {  
3         NSRunLoop *runLoop = [NSRunLoop currentRunLoop];  
4         [runLoop addTimer:unregisteredTimer forMode:NSDefaultRunLoopMode];  
5     }  
6 }

Initializing a Timer with a Fire Date

创建一个拥有指定发送日期的timer

 1 - (IBAction)startFireDateTimer:sender {  
 2     NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:1.0];  
 3     NSTimer *timer = [[NSTimer alloc] initWithFireDate:fireDate  
 4                                       interval:0.5  
 5                                       target:self  
 6                                       selector:@selector(countedtargetMethod:)  
 7                                       userInfo:[self userInfo]  
 8                                       repeats:YES];  
 9    
10     timerCount = 1;  
11     NSRunLoop *runLoop = [NSRunLoop currentRunLoop];  
12     [runLoop addTimer:timer forMode:NSDefaultRunLoopMode];  
13     [timer release];  
14 } 

Stopping a Timer

1 - (IBAction)stopRepeatingTimer:sender {  
2     [repeatingTimer invalidate];  
3     self.repeatingTimer = nil;  
4 }  

也可以从timer发送的消息中停止timer 

 1 - (void)countedtargetMethod:(NSTimer*)theTimer {  
 2    
 3     NSDate *startDate = [[theTimer userInfo] objectForKey:@"StartDate"];  
 4     NSLog(@"Timer started on %@; fire count %d", startDate, timerCount);  
 5    
 6     timerCount++;  
 7     if (timerCount > 3) {  
 8         [theTimer invalidate];  
 9     }  
10 }  

Memory Management

  1.  The run loop maintains the timer that is registered to it.
  2.  The timer is passed as an argument when you specify its method as a selector
  3.  You should maintain a strong reference to the unscheduled timer, in order to ensure that it's not deallocated before you use it.
  4.  A timer maintains a strong reference to its user info dictionary,
  5.  A timer maintains a strong reference to its target, so you should make sure that your timer's target survive longer than the timer itself.

转载于:https://www.cnblogs.com/ubersexual/archive/2013/03/26/2983472.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像中的目标属于哪个类别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像中的目标属于哪个类别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值