ios 如何将方法调用延迟 1 秒?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/920675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:58:18  来源:igfitidea点击:

How can I delay a method call for 1 second?

iosobjective-ciphonecocoa-touchuikit

提问by Thanks

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

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

I have a UIImageViewthat 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 animationDidStopselector.

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

回答by Jim

performSelector:withObject:afterDelay:

Document Reference

文件参考

回答by mcfedr

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 你可以使用全局 queue

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(...也可能是一个不错的选择。

回答by Milianoo

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 ];

回答by Alex Cio

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

已经有很多答案了,而且都是正确的。如果您想使用 ,dispatch_after您应该寻找包含在Code Snippet Library右下角(您可以选择UI元素的位置)内的代码段。

enter image description here

在此处输入图片说明

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

所以你只需要通过在代码中编写dispatch来调用这个片段:

enter image description here

在此处输入图片说明

回答by Nimit Parekh

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]; 

回答by Geri Borbás

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.

在这种情况下,您必须对某些视图进行一些更改才能使动画正常工作。它确实很hacky,但我喜欢基于块的东西。或者在下面结束@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(); });
}

回答by Nitesh

You can do this

你可以这样做

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

回答by Salman Ghumsani

Swift 2.x

斯威夫特 2.x

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")
}

or pass a escaping closure

或通过 escaping closure

func delay(seconds: Double, completion: @escaping()-> Void) {
    DispatchQueue.main.asyncAfter(deadline: .now() + seconds, execute: completion)
}

回答by Kevin ABRIOUX

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(){

}

回答by Sujeet Shrivastav

Use in Swift 3

在 Swift 3 中使用

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