zoukankan      html  css  js  c++  java
  • 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.
  • 相关阅读:
    【Yii2.0】1.5 Yii2.0新特性记录
    【PHP7.0】PHP7.0 小知识点摘录
    【PHP7.0】PHP7.0学习笔记目录
    【Yii2.0】1.4 Apache2.4.23基于主机名的虚拟主机配置
    【Yii2.0】2.2 Yii2.0 Basic代码中路由链接被转义的处理
    【Yii2.0】1.3 MySQL5.7.15修改root密码
    [Leetcode 106] 130 Surrounded Regions
    [Leetcode 105] 90 Subsets II
    [Leetcode 104] 131 Palindrome Partitioning
    [Leetcode 103] 37 Sudoku Solver
  • 原文地址:https://www.cnblogs.com/ubersexual/p/2983472.html
Copyright © 2011-2022 走看看