iphone NStimer start in 2 seconds

后端 未结 4 1630
终归单人心
终归单人心 2021-01-01 01:19

I am trying to have a block of code run 2 seconds after I \'start\' it.

I think the NSTimer can do this but can\'t figure it out.

相关标签:
4条回答
  • 2021-01-01 01:57

    You can also use some handy code :

    NSObject+PerformBlock.h

    @interface NSObject (PerformBlock)
    
    - (void)performBlock:(void(^)(void))block waitUntilDone:(BOOL)wait;
    - (void)performBlock:(void(^)(void))block afterDelay:(NSTimeInterval)delay;
    - (void)performBlock:(void(^)(void))block repeatCount:(NSUInteger)repeatCount timeInteval:(NSTimeInterval)timeInterval;
    
    @end
    

    NSObject+PerformBlock.m

    @interface NSObject (PerformBlockHidden)
    
    - (void)performBlock:(void(^)(void))block;
    
    @end
    
    @implementation NSObject (PerformBlock)
    
    - (void)performBlock:(void(^)(void))block {
        block();
    }
    
    - (void)performBlock:(void(^)(void))block waitUntilDone:(BOOL)wait {
        [self performSelector:@selector(performBlock:) onThread:nil withObject:[[block copy] autorelease] waitUntilDone:wait];
    }
    
    - (void)performBlock:(void(^)(void))block afterDelay:(NSTimeInterval)delay {
        [self performSelector:@selector(performBlock:) withObject:[[block copy] autorelease] afterDelay:delay];
    }
    
    - (void)performBlock:(void(^)(void))block repeatCount:(NSUInteger)repeatCount timeInteval:(NSTimeInterval)timeInterval {
        for (NSInteger repetition = 0; repetition < repeatCount; repetition++)
            [self performBlock:block afterDelay:(repetition*timeInterval)];
    }
    
    @end
    

    Then just import NSObject+PerformBlock.h and call :

    [myObject performBlock:^{
        // Code you want to perform after 2secs
    } afterDelay:2];
    
    0 讨论(0)
  • 2021-01-01 02:02

    The following will do what you need:

     NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 2 
                                       target:self 
                                       selector:@selector(handleTimer:) 
                                       userInfo:nil
                                       repeats:NO];
    

    Then the delegate function:

    -(void)handleTimer: (NSTimer *) timer 
    {       
       //code
    }
    
    0 讨论(0)
  • 2021-01-01 02:07

    NSTimer can be used, but another option is to use performSelector:withObject:afterDelay: It's basically like a method call (message send) that happens later.

    This example will send a doStuff: message after a delay:

    [self performSelector:@selector(doStuff:) withObject:self afterDelay:2];
    

    which causes this method to get invoked 2.0 seconds later:

    -(void)doStuff:(id)sender 
    {
        /// do something
    }
    
    0 讨论(0)
  • 2021-01-01 02:09

    You should be able to set the NSTimeInterval to 2.0 seconds and it should fire after that amount of time. What are you seeing? What is the code you are using to invoke the timer?

    0 讨论(0)
提交回复
热议问题