iphone NStimer 在 2 秒内启动

发布于 2024-09-01 08:12:57 字数 64 浏览 3 评论 0原文

我试图让一段代码在“启动”后 2 秒运行。

我认为 NSTimer 可以做到这一点,但无法弄清楚。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾`听者〃 2024-09-08 08:12:57

可以使用 NSTimer,但另一种选择是使用 performSelector:withObject:afterDelay: 基本上就像稍后发生的方法调用(消息发送)。

此示例将在延迟后发送一条 doStuff: 消息:

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

这会导致该方法在 2.0 秒后被调用:

-(void)doStuff:(id)sender 
{
    /// do something
}

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
}
过气美图社 2024-09-08 08:12:57

以下将满足您的需要:

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

然后是委托函数:

-(void)handleTimer: (NSTimer *) timer 
{       
   //code
}

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
}
此刻的回忆 2024-09-08 08:12:57

您还可以使用一些方便的代码:

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

然后只需导入 NSObject+PerformBlock.h 并调用:

[myObject performBlock:^{
    // Code you want to perform after 2secs
} afterDelay:2];

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];
萌无敌 2024-09-08 08:12:57

您应该能够将 NSTimeInterval 设置为 2.0 秒,并且它应该在该时间后触发。你看到什么了?您用来调用计时器的代码是什么?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文