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.
performSelector:withObject:afterDelay:
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.
dispatch_after
and press enter
dispatch_after
, this is really a super straightforward way to do it, plus you can pass in properties to the method you're calling, which is great.
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 ];
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).
https://i.stack.imgur.com/OSIvz.png
So you just need to call this snippet by writing dispatch in code:
https://i.stack.imgur.com/UP6gM.png
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];
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(); });
}
You can do this
[self performSelector:@selector(MethodToExecute) withObject:nil afterDelay:1.0 ];
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")
}
or pass a escaping closure
func delay(seconds: Double, completion: @escaping()-> Void) {
DispatchQueue.main.asyncAfter(deadline: .now() + seconds, execute: completion)
}
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(){
}
Use in Swift 3
perform(<Selector>, with: <object>, afterDelay: <Time in Seconds>)
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...
Success story sharing