Brian Coleman

  • Home
  • ABOUT
  • SERVICES
  • PORTFOLIO
  • BLOG
  • Contact

Tutorial: Run a Block of Code After a Delay

LED Baseball Pitch
For the longest time I’ve wanted to know how to pause the app or wait a certain amount of time before running a block of code. This is often useful for when you’re doing simple animations or if you need to wait before updating the UI. Take the example to the right, in the LED Baseball game I needed to find a way to animate the pitches from the mound to home plate. The LEDs images are in set spots, and for the effect of a pitch they need to blink on and off in sequence, sometimes at different speeds depending on the pitch.

The best way to implement this code block is to implement a category that can be reused in any of your projects.
The following code is credited to Duncan C.

The header file: (filename = NSObject+performBlockAfterDelay.h)

//
//  NSObject+performBlockAfterDelay.h
//
//  Copyright (c) 2012 WareTo. May be used by anyone, free of license, as 
//  long as this copyright notice remains.
//

@interface NSObject (performBlockAfterDelay)

- (void) performBlock: (dispatch_block_t) block
           afterDelay: (NSTimeInterval) delay;

@end

The .m file: (filename = NSObject+performBlockAfterDelay.m)

//
//  NSObject+performBlockAfterDelay.m
//  ChromaKey
//
//  Copyright (c) 2012 WareTo. May be used by anyone, free of license, as 
//  long as this copyright notice remains.
//

#import "NSObject+performBlockAfterDelay.h"

@implementation NSObject (performBlockAfterDelay)

- (void) performBlock: (dispatch_block_t) block
           afterDelay: (NSTimeInterval) delay;
{
  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC), dispatch_get_current_queue(), 
                 block);
}
@end

To use it, just add the .m and .h files above to your project.
Then add a #import at the top of any .m file that needs this method:

#import "NSObject+performBlockAfterDelay.h"

Here’s a sample of the code in action for a fastball in LED Baseball.

[self performBlock:^{led1.hidden = NO;} afterDelay: .75];
[self performBlock:^{led1.hidden = YES;led2.hidden = NO;} afterDelay: .15];
[self performBlock:^{led2.hidden = YES;led3.hidden = NO;} afterDelay: .225];
[self performBlock:^{led3.hidden = YES;led4.hidden = NO;} afterDelay: .3];
[self performBlock:^{led4.hidden = YES;led7.hidden = NO;} afterDelay: .375];
[self performBlock:^{led7.hidden = YES;led8.hidden = NO;} afterDelay: .45];
[self performBlock:^{led8.hidden = YES;led9.hidden = NO;} afterDelay: .525];

By running a series of blocks in a row, each LED is shown, then hidden after a set delay.

I hope this code helps you as much as it did me, I’ll be using this category anytime I need to “pause”.

Programmer Interview TipsMeet Trey Smith
Brian Coleman

Manager, Mobile Development at Rogers Communications with over 15 years of multifaceted experience including development, design, business analysis and project management working directly with clients in a consulting capacity throughout the full software life cycle.

June 26, 2013 Tutorialsios, objective-c, tutorial
Follow Me
    
Categories
  • About Me
  • Frameworks
  • My Apps
  • News
  • Strategy
  • Swift
  • Tools
  • Tutorials
  • tvOS
  • Uncategorized
  • Videos
  • Watch
Archives
  • May 2016
  • January 2016
  • October 2015
  • July 2015
  • May 2015
  • April 2015
  • March 2015
  • November 2014
  • October 2014
  • September 2014
  • July 2014
  • June 2014
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
Recent Posts
  • Classix for iPhone, iPad & Apple TV
  • Tutorial: How to test your app for IPv6 compatibility
  • Tutorial: Testing SSL using Charles Proxy on an iOS Device
  • Tutorial: 3D Touch – Quick Actions in Swift
  • tvOS Tutorial: Top Shelf in Swift
Featured Apps
Classix
Sportsnet
TAGS
tutorialswiftios8iosobjective-cvideogamesstrategynewsframeworkappsmonitizefacebookwatchappleios7toolstvosios9apiprovisionsocialtutorialsbooksdesignbookiapIPv6iTunes Connect
Search
TAGS
tutorialswiftios8iosobjective-cvideogamesstrategynewsframeworkappsmonitizefacebookwatchappleios7toolstvosios9apiprovisionsocialtutorialsbooksdesignbookiapIPv6iTunes Connect
ABOUT
Brian is a Lead iOS/tvOS Developer from Toronto with over 18 years of multifaceted experience including development, design, business analysis and project management.

FOLLOW ME
    
Email Subscription
Sign up for my newsletter to receive the latest news and tutorials posted.

Enter your email address:

2023 © Brian Coleman