Chris
Chris

Reputation: 27394

iPhone... Pause current thread between actions (non blocking)

I have a function that selects a row on a UITableView and then fires didSelectRowAtIndexPath to emulate the row having been clicked. This all happens very fast making it hard to see what has happened, I want to artificially pause between these actions so that it is more pleasing to the eye.

I implemented this using NSTimer using the following code however it only has a resolution as low as 1 second. I would ideally like to pause around 300 ms. How can I achieve as simply as possible?

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];

Upvotes: 0

Views: 111

Answers (1)

Sid
Sid

Reputation: 9536

I'm not sure I agree with calling didSelectRowAtIndexPath directly. I would suggest moving whatever you're doing in there to a separate method (say, selectionResponse:) and calling that directly.

Once you've done this, you can use performSelector:withObject:afterDelay: to call your selectionResponse: method, setting the afterDelay: argument to whatever value you want.

The link for the documentation on performSelector:withObject:afterDelay is here

Note that it's also a good idea to use + cancelPreviousPerformRequestsWithTarget:selector:object: (from the same document) in your dealloc, to cancel a pending perform request if your user chooses to back out of your view controller within the delay period, before the selector is invoked. This will prevent a crash.

Upvotes: 1

Related Questions