可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Is there an easy way delay a method call for 1 second?
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.
回答1:
performSelector:withObject:afterDelay:
Document Reference
回答2:
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.
Edit:
Swift 3 version:
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
object.method()
}
Equally, DispatchQueue.global().asyncAfter(...
might also be a good option.
回答3:
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.
example :
[self performSelector:@selector(subscribe) withObject:self afterDelay:3.0 ];
回答4:
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).
So you just need to call this snippet by writing dispatch in code:
回答5:
You can use the perform selector for after the 0.1 sec delay method is call for that following code to do this.
[self performSelector:@selector(InsertView) withObject:nil afterDelay:0.1];
回答6:
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.
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:
You can do this
[self performSelector:@selector(MethodToExecute) withObject:nil afterDelay:1.0 ];
回答8:
Swift 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
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
print("do some work")
}
回答9:
There is a Swift 3 solution from the checked solution :
self.perform(#selector(self.targetMethod), with: self, afterDelay: 1.0)
And there is the method
@objc fileprivate func targetMethod(){
}
回答10:
Use in Swift 3
perform(<Selector>, with: <object>, afterDelay: <Time in Seconds>)
回答11:
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?
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...