IT tutorials
 
Mobile
 

Iphone Application : Sensing Orientation and Motion - Detecting Tilt and Rotation (part 1)

11/22/2012 4:10:09 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
In the Orientation application, we ignored the precise values coming from the accelerometer and instead just allowed iOS to make an all-or-nothing orientation decision. The gradations between these orientations, such as the device being somewhere between its left side and straight up and down, are often interesting to an application.

Imagine you are going to create a car racing game where the device acts as the steering wheel when tilted left and right and the gas and brake pedals when tilted forward and back. It is very helpful to know how far the player has turned the wheel and how hard the user is pushing the pedals to know how to make the game respond.

Likewise, consider the possibilities offered by the gyroscope’s rotation measurements. Applications can now tell if the iPhone is rotating, even if there is no change in tilt. Imagine a turn-based game that switches between players just by rotating the iPhone around while it is lying on a table or sitting in an iPhone charging dock.

Setting Up the Project

In our next example application, ColorTilt, we take a solid color and make it progressively more transparent as the user tilts the device left or right or as they rotate the iPhone faster. We’ll add two toggle switches (UISwitch) to the view to enable/disable the accelerometer and gyroscope.

It’s not as exciting as a car racing game, but it is something we can accomplish in an hour, and everything learned here will apply when you get down to writing a great iPhone motion-enabled application.

Begin by creating a new View-Based iPhone Application in Xcode and calling it ColorTilt.

Adding the Core Motion Framework

Because this project relies directly on Core Motion, we’ll need to add the Core Motion framework for our code to work. Right-click the Frameworks group in Xcode, and choose Add, Existing Frameworks.

Choose CoreMotion.framework from the list that appears, and then click the Add button, as shown in Figure 1.

Figure 1. Add the Core Motion framework to the group.

Adding Outlets, Actions, and Properties

Our next step is to update the ColorTiltViewController.h file with the appropriate outlets and actions that we’ll need for the application.

Specifically, you should add an outlet for a UIView that will change colors—colorView, as well as two UISwitch instances—toggleAccelerometer and toggleGyroscope. We’ll also need an instance variable for our CMMotionManager object, which we’ll call motionManager.

Create corresponding @property declarations for each of these so we can access them easily from our application. As a final step, add an IBAction called controlHardware that will be used to enable or disable accelerometer and gyroscope readings.

The ColorTiltViewController.h file should read as shown in Listing 1.

Listing 1.
#import <UIKit/UIKit.h>
#import <CoreMotion/CoreMotion.h>

@interface ColorTiltViewController : UIViewController {
    IBOutlet UISwitch *toggleAccelerometer;
    IBOutlet UISwitch *toggleGyroscope;
    IBOutlet UIView *colorView;
    CMMotionManager *motionManager;
}

-(IBAction)controlHardware:(id)sender;

@property (nonatomic, retain) CMMotionManager *motionManager;
@property (nonatomic, retain) UISwitch *toggleAccelerometer;
@property (nonatomic, retain) UISwitch *toggleGyroscope;
@property (nonatomic, retain) UIView *colorView;

@end

For each of the @property declarations, we need a corresponding @synthesize line in the ColorTiltViewController.m implementation file. Add the following lines immediately following the @implementation line:

@synthesize motionManager;
@synthesize toggleAccelerometer;
@synthesize toggleGyroscope;
@synthesize colorView;

Next, let’s make sure we clean up after ourselves by releasing these objects in the ColorTiltViewController.m’s dealloc method:

- (void)dealloc {
    [motionManager release];
    [toggleAccelerometer release];
    [toggleGyroscope release];
    [colorView release];
    [super dealloc];
}

Preparing the Interface

Like the Orientation application, the ColorTilt application’s interface isn’t a work of art. It requires a few switches, labels, and a view. Open the ColorTiltViewController.xib file in Interface Builder by double-clicking it.

Lay out the user interface as follows:

1.
Add two UISwitch instances at the top-right of the view, one above the other. Use the Attributes Inspector (Command+1) to set each switch to Off by default.

2.
Add two labels (UILabel), Accelerometer and Gyroscope, to the view, positioned beside each switch.

3.
Drag a UIView instance into the view and size it to fit in the view below the switches and labels. Use the Attributes Inspector to change the view’s background to green.

Your view should now resemble Figure 2. If you feel like arranging the controls differently, feel free!

Figure 2. Create a layout that includes two switches, two labels, and a color view.

To connect the interface to the outlets defined earlier, begin by Control-dragging from the File’s Owner icon to the UIView, as shown in Figure 3. Choose colorView when prompted. Repeat the process for the two switches, connecting toggleAccelerometer to the switch beside the Accelerometer label and toggleGyroscope to the switch by the Gyroscope label.

Figure 3. Connect the objects to outlets you defined.

To finish the interface, the two switches need to be configured to invoke the controlHardware method when their Value Changed event occurs. Open the Document window so that the File’s Owner icon is visible. Next, select each switch, open the Connections Inspector (Command+2), and then drag from the circle beside Value Changed to the File’s Owner icon and choose controlHardware when prompted. (Yes, both switches connect to the same action!)

Save the XIB file and return to Xcode when finished.

 
Others
 
- Iphone Application : Sensing Orientation and Motion - Sensing Orientation
- Iphone Application : Sensing Orientation and Motion - Accessing Orientation and Motion Data
- Iphone Application : Sensing Orientation and Motion - Understanding iPhone Motion Hardware
- Buyer’s Guide : Android Device (Part 3) - Asus Eee Pad Transformer Prime, Samsung Galaxy Tab 2 10.1, Asus Eee Pad Transformer Infinity
- Buyer’s Guide : Android Device (Part 2) - Sony Tablet S, Acer Iconia Tab A200, Lenovo ThinkPad Tablet
- Buyer’s Guide : Android Device (Part 1) - Google Nexus 7, Samsung Galaxy tab 2 7.0
- Best Mid-Range iPhone Cases
- iWALK Chameleon
- Western Digital My Passport Edge 500GB
- Apple AirPort Express (2012)
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us