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]];
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 yourselectionResponse:method, setting theafterDelay:argument to whatever value you want.The link for the documentation on
performSelector:withObject:afterDelayis hereNote 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.