如何延迟一个方法调用1秒?

时间:2022-06-14 14:36:50

Is there an easy way delay a method call for 1 second?

是否有一种简单的方法将方法调用延迟1秒?

I have a UIImageView that reacts on a touch event. When the touch is detected, some animations happen in the app. After one second, I want to call another method. In this case, I can't use the animationDidStop selector.

我有一个UIImageView在触摸事件上发生反应。当触摸被检测到时,app中出现了一些动画。一秒钟后,我想调用另一个方法。在这种情况下,我不能使用animationDidStop选择器。

11 个解决方案

#1


249  

performSelector:withObject:afterDelay:

Document Reference

文档引用

#2


176  

You could also use a block

你也可以用块

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
    [object method]; 
});

Most of time you will want to use dispatch_get_main_queue, although if there is no UI in the method you could use a global queue.

大多数时候,您将希望使用dispatch_get_main_queue,但是如果方法中没有UI,您可以使用全局队列。

Edit:

编辑:

Swift 3 version:

斯威夫特3版本:

DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
    object.method()
}

Equally, DispatchQueue.global().asyncAfter(... might also be a good option.

同样,DispatchQueue.global().asyncAfter(…可能也是一个不错的选择。

#3


124  

Best way to do is :

最好的办法是:

[self performSelector:@selector(YourFunctionName) 
           withObject:(can be Self or Object from other Classes) 
           afterDelay:(Time Of Delay)];

you can also pass nil as withObject parameter.

还可以将nil作为withObject参数传递。

example :

例子:

[self performSelector:@selector(subscribe) withObject:self afterDelay:3.0 ];

#4


21  

There are already a lot of answers and they are all correct. In case you want to use the dispatch_after you should be looking for the snippet which is included inside the Code Snippet Library at the right bottom (where you can select the UI elements).

已经有很多答案了,而且都是正确的。如果您想要使用dispatch_after,您应该寻找在右底部的代码片段库中包含的代码片段(在这里您可以选择UI元素)。

如何延迟一个方法调用1秒?

So you just need to call this snippet by writing dispatch in code:

所以你只需要用代码编写分派来调用这个代码片段:

如何延迟一个方法调用1秒?

#5


17  

You can use the perform selector for after the 0.1 sec delay method is call for that following code to do this.

您可以在0.1秒的延迟方法之后使用执行选择器来执行以下代码。

[self performSelector:@selector(InsertView)  withObject:nil afterDelay:0.1]; 

#6


8  

You can also:

您还可以:

[UIView animateWithDuration:1.0
                 animations:^{ self.view.alpha = 1.1; /* Some fake chages */ }
                 completion:^(BOOL finished)
{
    NSLog(@"A second lapsed.");
}];

This case you have to fake some changes to some view to get the animation work. It is hacky indeed, but I love the block based stuff. Or wrap up @mcfedr answer below.

在这种情况下,您必须对某些视图进行一些修改才能使动画工作。这确实是一种黑客行为,但我喜欢基于块的东西。或者在下面结束@mcfedr的回答。


waitFor(1.0, ^
{
    NSLog(@"A second lapsed");
});

typedef void (^WaitCompletionBlock)();
void waitFor(NSTimeInterval duration, WaitCompletionBlock completion)
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC),
                   dispatch_get_main_queue(), ^
    { completion(); });
}

#7


7  

You can do this

你可以这样做

[self performSelector:@selector(MethodToExecute) withObject:nil afterDelay:1.0 ];

#8


1  

Swift 2.x

快2.倍

let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC)))
 dispatch_after(delayTime, dispatch_get_main_queue()) {
 print("do some work")
}

Swift 3.x --&-- Swift 4

斯威夫特3。x - & -斯威夫特4

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
    print("do some work")
}

#9


0  

There is a Swift 3 solution from the checked solution :

从检查的解决方案中有一个Swift 3的解决方案:

self.perform(#selector(self.targetMethod), with: self, afterDelay: 1.0)

And there is the method

这就是方法

@objc fileprivate func targetMethod(){

}

#10


0  

Use in Swift 3

使用快速3

perform(<Selector>, with: <object>, afterDelay: <Time in Seconds>)

#11


-36  

NOTE: this will pause your whole thread, not just the one method.
Make a call to sleep/wait/halt for 1000 ms just before calling your method?

注意:这将暂停整个线程,而不仅仅是一个方法。在给你的方法打电话之前,打电话给你的睡眠/等待/停止1000毫秒?

Sleep(1000); // does nothing the next 1000 mSek

Methodcall(params); // now do the real thing

Edit: The above answer applies to the general question "How can I delay a method call for 1 second?", which was the question asked at the time of the answer (infact the answer was given within 7 minutes of the original question :-)). No Info was given about the language at that time, so kindly stop bitching about the proper way of using sleep i XCode og the lack of classes...

编辑:上面的答案适用于“如何将方法调用延迟1秒?”,这是在回答的时候提出的问题(事实上,答案是在最初问题的7分钟内给出的:-))。当时没有关于语言的信息,所以请不要抱怨使用睡眠的正确方法,因为我没有课……

#1


249  

performSelector:withObject:afterDelay:

Document Reference

文档引用

#2


176  

You could also use a block

你也可以用块

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
    [object method]; 
});

Most of time you will want to use dispatch_get_main_queue, although if there is no UI in the method you could use a global queue.

大多数时候,您将希望使用dispatch_get_main_queue,但是如果方法中没有UI,您可以使用全局队列。

Edit:

编辑:

Swift 3 version:

斯威夫特3版本:

DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
    object.method()
}

Equally, DispatchQueue.global().asyncAfter(... might also be a good option.

同样,DispatchQueue.global().asyncAfter(…可能也是一个不错的选择。

#3


124  

Best way to do is :

最好的办法是:

[self performSelector:@selector(YourFunctionName) 
           withObject:(can be Self or Object from other Classes) 
           afterDelay:(Time Of Delay)];

you can also pass nil as withObject parameter.

还可以将nil作为withObject参数传递。

example :

例子:

[self performSelector:@selector(subscribe) withObject:self afterDelay:3.0 ];

#4


21  

There are already a lot of answers and they are all correct. In case you want to use the dispatch_after you should be looking for the snippet which is included inside the Code Snippet Library at the right bottom (where you can select the UI elements).

已经有很多答案了,而且都是正确的。如果您想要使用dispatch_after,您应该寻找在右底部的代码片段库中包含的代码片段(在这里您可以选择UI元素)。

如何延迟一个方法调用1秒?

So you just need to call this snippet by writing dispatch in code:

所以你只需要用代码编写分派来调用这个代码片段:

如何延迟一个方法调用1秒?

#5


17  

You can use the perform selector for after the 0.1 sec delay method is call for that following code to do this.

您可以在0.1秒的延迟方法之后使用执行选择器来执行以下代码。

[self performSelector:@selector(InsertView)  withObject:nil afterDelay:0.1]; 

#6


8  

You can also:

您还可以:

[UIView animateWithDuration:1.0
                 animations:^{ self.view.alpha = 1.1; /* Some fake chages */ }
                 completion:^(BOOL finished)
{
    NSLog(@"A second lapsed.");
}];

This case you have to fake some changes to some view to get the animation work. It is hacky indeed, but I love the block based stuff. Or wrap up @mcfedr answer below.

在这种情况下,您必须对某些视图进行一些修改才能使动画工作。这确实是一种黑客行为,但我喜欢基于块的东西。或者在下面结束@mcfedr的回答。


waitFor(1.0, ^
{
    NSLog(@"A second lapsed");
});

typedef void (^WaitCompletionBlock)();
void waitFor(NSTimeInterval duration, WaitCompletionBlock completion)
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC),
                   dispatch_get_main_queue(), ^
    { completion(); });
}

#7


7  

You can do this

你可以这样做

[self performSelector:@selector(MethodToExecute) withObject:nil afterDelay:1.0 ];

#8


1  

Swift 2.x

快2.倍

let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC)))
 dispatch_after(delayTime, dispatch_get_main_queue()) {
 print("do some work")
}

Swift 3.x --&-- Swift 4

斯威夫特3。x - & -斯威夫特4

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
    print("do some work")
}

#9


0  

There is a Swift 3 solution from the checked solution :

从检查的解决方案中有一个Swift 3的解决方案:

self.perform(#selector(self.targetMethod), with: self, afterDelay: 1.0)

And there is the method

这就是方法

@objc fileprivate func targetMethod(){

}

#10


0  

Use in Swift 3

使用快速3

perform(<Selector>, with: <object>, afterDelay: <Time in Seconds>)

#11


-36  

NOTE: this will pause your whole thread, not just the one method.
Make a call to sleep/wait/halt for 1000 ms just before calling your method?

注意:这将暂停整个线程,而不仅仅是一个方法。在给你的方法打电话之前,打电话给你的睡眠/等待/停止1000毫秒?

Sleep(1000); // does nothing the next 1000 mSek

Methodcall(params); // now do the real thing

Edit: The above answer applies to the general question "How can I delay a method call for 1 second?", which was the question asked at the time of the answer (infact the answer was given within 7 minutes of the original question :-)). No Info was given about the language at that time, so kindly stop bitching about the proper way of using sleep i XCode og the lack of classes...

编辑:上面的答案适用于“如何将方法调用延迟1秒?”,这是在回答的时候提出的问题(事实上,答案是在最初问题的7分钟内给出的:-))。当时没有关于语言的信息,所以请不要抱怨使用睡眠的正确方法,因为我没有课……