How do you develop apps for iOS?  It’s a common question that many computer enthusiasts ask, and one we here at TechnoBuffalo hope to answer with this series of posts.

Learning how to develop software can be one of the most intimidating prospects for the average computer user, and with the growing saturation of applications in mobile marketplaces, it is becoming increasingly difficult to get your work noticed. That’s what this series is for, helping you learn iOS development from a conceptual perspective. No prior knowledge of computer programming will be necessary. Over the coming weeks, we’ll examine the iPhone’s ability to deliver immersive, intuitive content, a unique opportunity for both developers and consumers.

Today, we’ll be utilizing the accelerometer housed within every iOS device. These small instruments can measure the relative motion of any given gadget.

1. First, we must open up Xcode 4, Apple’s latest Integrated Development Environment. Select New Project and follow the on-screen prompts until you are met with the template page. Select View-based Application and make sure that you are targeting the iPhone.

2.  After following Xcode’s instructions on setting up the application, entitle the program Accelerometer and save the file in a convenient place, preferably one that can be easily remembered and accessed in the future.

3.  Today, we will begin by throwing the object that we will eventually control with the accelerometer into the program. Snag the picture that we have posted to the right and drag it into your application’s resource files.

4.  Next, we need to manipulate the view controller’s header file (AccelerometerViewController.h). Copy the code below, which initializes the area that we will eventually control using the accelerometer.

————————————————————–

#import <UIKit/UIKit.h>
#define BALL_RADIUS 25

@interface AccelerometerViewController : UIViewController <UIAccelerometerDelegate> {

IBOutlet UIImageView *ball;
float valueX;
float valueY;

}

@end

————————————————————–

5.  Now it is time to head on over to the implementation view controller (AccelerometerViewController.m). This file will further define the code that we initialized in the header, giving it specific characteristics that will allow for the interpretation of user input.

————————————————————–

#import “AccelerometerViewController.h”

@implementation AccelerometerViewController

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

valueX = acceleration.x*30.0;
valueY = acceleration.x*30.0;

int newX = (int)(ball.center.x + valueX);

if (newX > 320-BALL_RADIUS)
newX = 320-BALL_RADIUS;

if (newX < 0 + BALL_RADIUS)
newX = 0 + BALL_RADIUS;

int newY = (int)(ball.center.y-valueY);

if (newY > 460 – BALL_RADIUS)
newY = 460 – BALL_RADIUS;

if (newY < 0 + BALL_RADIUS)
newY = 0 + BALL_RADIUS;

CGPoint newCenter = CGPointMake(newX, newY);
ball.center = newCenter;

}

- (void)viewDidLoad {

[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0/30.0];
[[UIAccelerometer sharedAccelerometer] setDelegate:self]; [super viewDidLoad];
}

- (void)dealloc
{
[super dealloc];
}

————————————————————–

6.  That does it for the coding portion of our application. Now, it’s time to head on over to the interface file, AccelerometerViewController.xib. Here, we will connect our code with the image. First, drag a UIImageView onto the application’s interface canvas. Then, go to the “Media” tab and drag our smiley face into the shot. Double-click on File’s Owner and drag from Ball to the image that we created.

 

8.  Build and run! The smiley face will not turn in the iOS Simulator because there is technically no accelerometer, but if there are no compilation errors, you have completed this tutorial effectively.

Questions?

If you have any questions about the topics discussed in this lesson, feel free to voice them in the comments below. We will do our best to ensure that you have a relatively painless experience developing for iOS. Stay tuned for the next installment of this series, which is released weekly.

To read previous installments from this series, check the links below.