Reputation: 12446
I know of:
performSelector:afterDelay:
and these, but I always have to make an extra method for every delay.
In Cocos2d-iphone
I use a CCAction
for that CCTimeDelay
, within a CCSequence
. Would be cool if it would also work outside of Cocos2d
.
If I want to wait, lets say 1.3 seconds and don't want to split the method in half because of that, what should I do?
I know when I want to wait and don't change the context, the thread will do nothing. So the performSelector methods are good for that.
How to make it happen, that not the whole app waits, userInput should be accepted while waiting. Let's say I want to create an animation of two steps, in between there should be an time interval of 0.8 seconds. To keep it in one method but use multithreading I use blocks then (any other idea appreciated). How to wait inside the block, so the main thread won't get disturbed?
Upvotes: 0
Views: 228
Reputation: 7704
What about GCD (Grand Central Dispatch) way of performing blocks? It will allow you to have the code at the same place, without extra methods.
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// Do whatever you want
});
You can replace dispatch_get_main_queue()
with your own queue. Read more at Raywenderlich
You don't need to remember all this stuff. Just start writing dispatch_after
inside method and Xcode's code sense will offer you code snippet to auto generate (as MartinR pointed in comment on the question)
Upvotes: 3
Reputation: 19872
You can do it using:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC*delay),
dispatch_get_current_queue(), block);
Upvotes: 1