Show Menu

Looking to hire an app developer?

Submit your 30 day Job Listing for FREE

xcode bomb image

In this tutorial I will be showing you have to create a basic iPhone Application that has an object, in this case an image, which falls from the top of the screen at a random location.

Updated version can be found here: iOS 7: how to use UIGravityBehavior tutorial

If you would rather follow along the video tutorial please do so by watching it below. At the bottom of the tutorial you can also download the source files for this project.

If you cannot see the video in your browser, please visit Youtube to view it.

Let’s get started

The first thing you need to do is create a new Xcode Single View Application.

Xcode Single View Application

Then click the next button at the bottom right of this popup, you will then be presented with another screen asking you to chose options for your application. I am going to aptly name this project: FallingObject. For the purpose of this project the organisation name can be anything you want and I have used com.ios-blog as the company identifier, but again this can be anything that you want.

Xcode options dialogue

You will then be asked to save your Xcode project. Choose a suitable location and hit save.

The first file we need to edit is the application header file: viewController.h. On the left hand side of the screen you will see project navigator with a list of the files associated with this project.

xcode project navigator

Click the viewController.h file and it should open up.

So, now to the coding bit , we need to declare a few things in this header file. Enter this code in-between your @interface ViewController : UIViewController

CGPoint position;
IBOutlet UIImageView *image;
NSTimer *timer;

If you need more information on the NSTimer Method check out this tutorial: Objective-C: Using NSTimer

Next, I need to declare two IBActions for the buttons that will be the initiator and the terminator of the object falling. So, out of your @interface declare:


We now need to head back to our Project Navigator and select the implementation file: viewController.m and code in our IBActions. This is where we also implement our NSTimer as we need to tell the object to keep moving down. Just above the – (void)viewDidLoad and below the @implementation ViewController we need to add:

-(IBAction)Start {
    timer = [NSTimer scheduledTimerWithTimeInterval:(0.1)
             target: self
             userInfo: nil repeats: YES];

-(IBAction)Stop {
    if (timer != nil)
        [timer invalidate];
        timer = nil;

-(void)onTimer { = CGPointMake(,;
    position = CGPointMake(0.0, 0.15);

If you look at the documentation for NSTimer you will find out that the number after scheduledTimerWithTimeInterval is the number of seconds between firings of the timer. If seconds is less than or equal to 0.0, this method chooses the nonnegative value of 0.1 milliseconds instead. Download the source files for this project below and change the value of this and see the effects.

Drag and Drop in the image that you have chosen to use in the project navigator. I have used a free no back-link required image from icon finder. You will then have a dialogue window pop up. Make sure that you have the copy items to destination’s group folder option selected:

xcode copy to destination

This is the image that I used:


Image found: iconfinder search

Please Make sure that you double check and then check again that the image provided is still royalty free. You do not want to get sued when making your app.

Now, we need to go into the mainstoryboard in the project navigator and drag in the image that we just added to the project and two UIButtons. Rename the buttons Start and Stop like so:

xcode storyboard

Next, press down the Ctrl button, click and hold with your mouse pad over the view controller button at the button and drag up to and release over the image:

xcode link to files owner

Another Dialogue will then open up, click image:

xcode iboutlet UIImageView

Next, you need to do the same for the buttons that we created, here is the Start button:

xcode link files owner

Another window will then pop up and you should select: Start:

xcode files owner

Follow those steps for the stop button, making sure you select stop when the list of options comes available.

That’s it. Run your project and in the simulator you should now see your application ready to do.

Source files for this project

Download the source files, mess around and have some fun with your application.

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly

Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.

iOS-Blog Admin Team

Written by:

We're here to help.