IT tutorials
 
Mobile
 

Iphone Application : Sensing Orientation and Motion - Sensing Orientation

11/22/2012 4:06:43 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
As our first introduction to detecting motion, we’ll create the Orientation application. Orientation won’t be wowing users, it’s simply going to say which of six possible orientations the iPhone is currently in. The Orientation application will detect standing-up, upside-down, left-side, right-side, face-down, and face-up orientations.

Setting Up the Project

Create a new view-based iPhone application in Xcode and call it Orientation. Click the OrientationViewController.h file in the Classes group and add an outlet property (orientationLabel) for an orientation label. The OrientationViewController.h file should read as shown in Listing 1.

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

@interface OrientationViewController : UIViewController {
    IBOutlet UILabel *orientationLabel;
}

@property (nonatomic, retain) UILabel *orientationLabel;

@end

Add the appropriate @synthesize directive following the @implementation line:

@synthesize orientationLabel;

Finally, release the label in the implementation file’s dealloc method:

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

Preparing the Interface

Orientation’s UI is simple (and very stylish), just a yellow text label in a field of black, which we’ll construct as follows:

1.
Open Interface Builder by double-clicking the OrientationViewController.xib file in the Resources group.

2.
Click the empty view and open the Attributes Inspector (Command+1).

3.
Click the Background attribute and change the view’s background to black.

4.
Open the Library (Shift+Command+L) and drag a label (UILabel) to the center of the view; expand the size the label to the edge sizing guidelines on each side of the view. Set the label’s text to read Face Up.

5.
With the label selected, open the Attributes Inspector (Command+1).

6.
Click the Color attribute and change the label’s text color to yellow, use the Alignment attribute to center the label’s text, and then update the Font attribute to change the font size to 36 points.



The finished view should look like Figure 1.

Figure 1. The Orientation application’s UI in Interface Builder.


Our application will need to be able to change the text of the label when the accelerometer indicates that the orientation of the device has changed. To do this, we’ll need to connect the outlet we created earlier to the label.

Control-drag from the File’s Owner icon in the Document window to the label in the view. Choose the orientationLabel outlet when prompted. Save the XIB file and return to Xcode.

Reacting to Changes in Orientation

When our custom view is shown, we need to register a method in our application to receive UIDeviceOrientationDidChangeNotification notifications from iOS. We also need to tell the device itself that it should begin generating these notifications so that we can react to them. All of this setup work can be accomplished in the Orientation.m viewDidLoad method. Let’s implement that now. Uncomment and edit the viewDidLoad method to read as shown in Listing 2.

Listing 2.
 1: - (void)viewDidLoad {
 2:
 3:     [[UIDevice currentDevice]beginGeneratingDeviceOrientationNotifications];
 4:
 5:     [[NSNotificationCenter defaultCenter]
 6:                     addObserver:self selector:@selector(orientationChanged:)
 7:                     name:@"UIDeviceOrientationDidChangeNotification"
 8:                     object:nil];
 9:
10:     [super viewDidLoad];
11: }

					  

In line 3, we use the method [UIDevice currentDevice] to return an instance of UIDevice that refers to the iPhone our application is running on. We then use the beginGeneratingDeviceOrientationNotifications to tell the device that we’re interested in hearing about it if the user changes the orientation of his or her phone.

Lines 5–8 tell the NSNotificationCenter object that we are interested in subscribing to any notifications with the name UIDeviceOrientationDidChangeNotification that it may receive. We also tell that the class that is interested in the notifications is OrientationViewController by way of the addObserver:self message, and, finally, we use @selector(orientationChanged:) to say that we will be implementing a method called orientationChanged. In fact, coding up orientationChanged is the only thing left to do!

Determining Orientation

To determine the orientation of the device, we’ll use the UIDevice property orientation.The orientation property is of the type UIDeviceOrientation (simple constant, not an object). This means that we can check each possible orientation via a simple switch statement and update the orientationLabel in the interface as needed.

Implement the orientationChanged method as shown in Listing 3.

Listing 3.
 1: - (void)orientationChanged:(NSNotification *)notification {
 2:
 3:     UIDeviceOrientation orientation;
 4:     orientation = [[UIDevice currentDevice] orientation];
 5:
 6:     switch (orientation) {
 7:         case UIDeviceOrientationFaceUp:
 8:             orientationLabel.text=@"Face Up";
 9:             break;
10:         case UIDeviceOrientationFaceDown:
11:             orientationLabel.text=@"Face Down";
12:             break;
13:         case UIDeviceOrientationPortrait:
14:             orientationLabel.text=@"Standing Up";
15:             break;
16:         case UIDeviceOrientationPortraitUpsideDown:
17:             orientationLabel.text=@"Upside Down";
18:             break;
19:         case UIDeviceOrientationLandscapeLeft:
20:             orientationLabel.text=@"Left Side";
21:             break;
22:         case UIDeviceOrientationLandscapeRight:
23:             orientationLabel.text=@"Right Side";
24:             break;
25:         default:
26:             orientationLabel.text=@"Unknown";
27:             break;
28:     }
29: }

					  

Run the application when complete. Your results should resemble Figure 2.

Figure 2. Orientation in action.

 
Others
 
- 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)
- The Life Of An App
 
 
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