Browse By

iPhone: Create Tapping Game programmatically using Object C

Hello, friends… I hope you have learned a lot from my previous articles. In Last article we have learned how to customize the Text of UILabel Programmatically. Today we are going to create an interesting Tapping Game programmatically using NSTimer, Enable/Disable property of Object. :-)

iPhone: Create Tapping Game programmatically using Object CWhat is Tapping Game?

The Tapping game is that when we press the “Start Game” button a counter will start decreasing number. As the count reaches 0 we won’t be able to interact with the button. By the time the counter reaches to 0, we have to press another button rapidly. When we press on “Tap me” Button the score will increase.

What is NSTimers?

NSTimers are like a loop statement that perform or triggers a void statement over and over again for a certain amount of time. The instructions in the void statements will be performed as per requirement.

In this example we are going to use NSTimers to make a counter. When we press a button, it will display number on a UILabel and decrease the count.

Direct Download Full Source Code

iPhone: Create Tapping Game programmatically using Object C

To create this nice and simple game, perform following steps. First of all think for about the user interface elements you will need for your button-tapping game:

  1.  A button to start game – Start Game
  2.  Something to indicate how much time is left – Timer
  3.  The player’s current score – Score
  4.  A button to tap – Tap me
  1. Create user interface as shown in the below image using UIButton and UILabel.

Tapping Game

2. Bring up the Assistant Editor by pressing below Button located at upper right corner and make following connections with ViewController.h file.Assistant Editor Button

Outlet named “timeLabel” for our first UILabel that will be used to display time decreasing. Outlet named “tapLabel” for our second UILabel that will be used to display tap count or score according to your tapping speed.

Outlet for the first UIButton named “tapOutletButton” that will be used to enable or disable the button according to the game situation

Action named “startGame” for the first UIButton that will be used to start the game. Action named “tapActionButton” for the second UIButton that will be used to increase the score.

3. Create braces {} after UIViewController in ViewController.h file and type the code shown below within it.

NSTimer *timer;
int timeInt;
int tapInt;

Here we declare variables that will be used in further code. NSTimer to create a counter, timeInt for count down and tapInt for score.

4. Navigate to ViewController.m file and type following code of snippet in the viewDidLoad method to give starting value to both integers.

timeInt = 5;
tapInt =0;

self.tapOutletButton.enabled = NO;

Here we set timeInt to 5 because we want to run the game for 5 seconds after we press the Start Game. tapInt is set to 0 from where the tap count will begin. We disable the tap button initially so that it cannot be interacted until the game is started.

5. Add the code shown below within action method named “startGame”. Here we start the game by displaying initial values of time and score. Time will decrease by 1 and displayed per second on the view. Void function is created in next step that is called from the selector.

 self.timeLabel.text = [NSString stringWithFormat:@"%i", timeInt];
   self.tapLabel.text = [NSString stringWithFormat:@"%i", tapInt];
    if (timeInt == 5) {
        [[sender setTitle:@"Restart" forState: UIControlStateNormal];
        self.tapOutletButton.enabled = YES;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self     selector:@selector(startCounter) userInfo:nil repeats:YES];

Time interval 1.0 means 1 second is taken for NSTimeInterval because we want to display the result every second. We will take nil for userInfo and YES for repeats because we want to repeat the statement. Every second, the void statement given in selector will be triggered.

6. Create timer function using the code shown below. Here we bring the UIButton and set a Boolean value that determines whether the receiver is enabled.

-(void) startCounter {
    timeInt -= 1;
self.timeLabel.text = [NSString stringWithFormat:@"%i", timeInt];
    if (timeInt == 0) {
        [timer invalidate];
        self.tapOutletButton.enabled = NO;
        timeInt = 5;
        tapInt = 0;

What we have done is:

Decrease the count and display it on the UILabel. Check if the count is 0 because if it is, use [Timer invalidate] to stop that decreasing in negative value and the values are again set to initial values in case we want to restart the game.

Disable the button to take the ability from user to interact with it. The object will be displayed on the view but we won’t be able to interact with it when it is disabled.

This is widely used in games to give ability to the user to go to next level or not. It is also used in application where users are not allowed to use some of the features of the product. Users need to purchase features to enable them.

7. Finally add the code shown below within the action method named “tapActionButton” to increase the count or score and display it.

tapInt += 1;
self.tapLabel.text = [NSString stringWithFormat:@"%i", tapInt];


The project is ready to build and run. You can play, restart the game and compare it with friends. The game be restarted by pressing the UIButton “Start Game”. The output is:

Tapping Game using NSTimer, Enable/Disable property of Object
Congratulations!! You have made your iOS Tapping game. Now you can challenge your friends and family members for highest score for Tapping Game. I hope you like this article. Share your views to improve content. Please stay tune with us. Happy Coding !!!

Subscribe for more articles!!

Download Full Source Code