Logo
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
EPL Standings
 
 
Windows Phone

Iphone Application : Using Gesture Recognizers (part 4)

- 2015 Chevrolet Camaro Z28 - The Legend Returns
- Wagon Audi Allroad Vs. Subaru Outback
- 996 Carrera 4S is Driving Perfection
11/8/2012 5:10:17 PM

Implementing the Rotation Gesture Recognizer

The last multitouch gesture recognizer that we’ll add is the rotation gesture recognizer. Like the pinch gesture, rotation returns some useful information that we can apply visually to our onscreen objects, notably velocity and rotation. The rotation returned is the number of radians that the user has rotated his or her fingers, clockwise or counterclockwise.

Did you Know?

Most of us are comfortable talking about rotation in “degrees,” but the Cocoa classes usually use radians. Don’t worry. It’s not a difficult translation to make. If you’d like, you can calculate degrees from radians using the following formula:

Degrees = Radians × 180 / Pi

There’s not really any reason we need to do this in this project, but in your own applications, you may want to provide a degree reading to your users.


One last time, edit the viewDidLoad method and add the following code fragment for the rotation recognizer:

UIRotationGestureRecognizer *rotationRecognizer;
rotationRecognizer=[[UIRotationGestureRecognizer alloc]
                 initWithTarget:self
                 action:@selector(foundRotation:)];
[rotateView addGestureRecognizer:rotationRecognizer];
[rotationRecognizer release];

The rotation gesture recognizer is added to the rotateView and set to trigger foundRotation when a gesture is detected.

Responding to the Recognizer

I’d love to tell you how difficult it is to rotate a view and about all the complex math involved, but I pretty much gave away the trick to rotation in the foundPinch method earlier. A single line of code will set the UIImageView’s transform property to a rotation transformation and visually rotate the view. Of course, we will also need to provide a feedback string to the user, but that’s not nearly as exciting, is it?

Add the foundRotation method in Listing 4 to your GesturesViewController.m file.

Listing 4.
 1: - (void)foundRotation:(UIRotationGestureRecognizer *)recognizer {
 2:     NSString *feedback;
 3:     double rotation;
 4:     rotation=recognizer.rotation;
 5:     feedback=[[NSString alloc]
 6:               initWithFormat:@"Rotated, Radians:%1.2f, Velocity:%1.2f",
 7:               recognizer.rotation,recognizer.velocity];
 8:     outputLabel.text=feedback;
 9:     imageView.transform = CGAffineTransformMakeRotation(rotation);
10:     [feedback release];
11: }

Again, we declare a feedback string and a floating-point value, this time rotation, in lines 2–3. Line 4 sets the rotation value to the recognizer’s rotation property. This is the rotation in radians detected in the user’s gesture.

Line 5 creates the feedback string showing the radians rotated and the velocity of the rotation, while line 8 sets the output label to the string.

Line 9 handles the rotation itself, creating a rotation transformation and applying it to the imageView object’s transform property.

Line 10 finishes up by releasing the feedback string.

Build and Run to test your application now. You should be able to freely spin the image view using a rotation gesture in the rotate view, as shown in Figure 7.

Figure 7. Spin the image view using the rotation gesture.


Although it might seem like we’ve finished, there’s still one gesture we need to cover: a shake.

Implementing the Shake Recognizer

Dealing with a shake is a bit different from the other gestures covered this hour. We must intercept a UIEvent of the type UIEventTypeMotion. To do this, our view controller or view must be the first responder in the responder chain and must implement the motionEnded:withEvent method.

Let’s tackle these requirements one at a time.

Becoming First Responder

For our view controller to be a first responder, we have to allow it and then ask for it. Add the following two methods to the GesturesViewController.m file:

- (BOOL)canBecomeFirstResponder {
    return YES; // For the shake event
}

- (void)viewDidAppear:(BOOL)animated {
    [self becomeFirstResponder]; // For the shake event
    [super viewDidAppear: animated];
}

Our view controller is now prepared to become the first responder and receive the shake event. All we need to do now is implement motionEnded:withEvent to trap and react to the shake itself.

Reacting to a Shake

To react to a shake, implement the motionEnded:withEvent method as shown in Listing 5.

Listing 5.
1: - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
2:     if (motion==UIEventSubtypeMotionShake) {
3:         outputLabel.text=@"Shaking things up!";
4:         imageView.transform = CGAffineTransformMakeRotation(0.0);
5:         imageView.frame=CGRectMake(originX,originY,originWidth,originHeight);
6:     }
7: }

					  

First things first: In line 2, we check to make sure that the motion value we received (an object of type UIEventSubtype) is, indeed, a motion event. To do this, we just compare it to the constant UIEventSubtypeMotionShake. If they match, the user just finished shaking the device!

Lines 3–4 react to the shake by setting the output label, rotating the image view back to its default orientation, and setting the image view’s frame back to the original size. In other words, shaking the iPhone will reset the image to its default state. Pretty nifty, huh?

You can now run the application and use all the gestures that we implemented this hour. Although not a useful app in and of itself, it does illustrate many techniques that you can use in your own applications.

Top Search -----------------
- Windows Server 2008 R2 : Work with RAID Volumes - Understand RAID Levels & Implement RAID
- Windows Server 2008 R2 Administration : Managing Printers with the Print Management Console
- Configuring Email Settings in Windows Small Business Server 2011
- Windows Server 2008 R2 : Configuring Folder Security, Access, and Replication - Implement Permissions
- Monitoring Exchange Server 2010 : Monitoring Mail Flow
- Windows Server 2008 R2 :Task Scheduler
- Windows Server 2008 R2 : File Server Resource Manager
- Windows Server 2008 R2 : Installing DFS
- Exchange Server 2010 : Managing Anti-Spam and Antivirus Countermeasures
- Windows Server 2008 R2 : Configuring Folder Security, Access, and Replication - Share Folders
Other -----------------
- Handling Input on Windows Phone 7 : Microphone Input
- Handling Input on Windows Phone 7 : Accelerometer
- XNA Game Studio 4.0 : XNA Game Studio Storage (part 2) - Getting a Device
- XNA Game Studio 4.0 : XNA Game Studio Storage (part 1) - Recreating the Project on Xbox, Devices and Containers
- XNA Game Studio 4.0 : Storage - Isolated Storage
- Using Media in XNA Game Studio : Visualizations
- Using Media in XNA Game Studio : Video
- XNA Game Studio 4.0 : Dynamic Sound Effects - Recording Audio with a Microphone, Generating Dynamic Sound Effects
- XNA Game Studio 4.0 : Playing Sound Effects (part 2) - Microsoft Cross-Platform Audio Creations Tool
- XNA Game Studio 4.0 : Playing Sound Effects (part 1) - Using SoundEffect for Audio Playback
 
 
Most view of day
- Microsoft Dynamic CRM 4 : Data Migration (part 4) - Creating a Data Migration
- Leveraging Social Networking Tools in SharePoint 2010 : Restricting User Access to and Creation of My Site Sites
- Microsoft Dynamics AX 2009 : The MorphX Tools - Code Compiler & Dynamics AX SDK
- Windows Server 2003 : Securing Network Communications Using IPSec - Troubleshooting Data Transmission Security
- Building BizTalk Server 2009 Applications : Setting up new BizTalk projects
- Iphone Application : Using Gesture Recognizers (part 4)
- Microsoft Dynamics AX 2009 : Working with Data in Forms - Processing multiple records
- Windows Server 2008 R2 : Elements of Group Policy (part 5)
- Maintaining and Troubleshooting Windows 7 : Using Windows RE (part 1) - Accessing the WinRE
- Windows Server 2008 R2 : Deploying and Using Windows Virtualization - Quick Migration and Live Migration (part 2)
Top 10
- Microsoft Exchange Server 2010 : Completing Transport Server Setup (part 8) - Configuring Transport Rules
- Microsoft Exchange Server 2010 : Completing Transport Server Setup (part 7) - Configuring Journal Rules
- Microsoft Exchange Server 2010 : Completing Transport Server Setup (part 6) - Verifying Edge Subscriptions, Removing Edge Subscriptions
- Microsoft Exchange Server 2010 : Completing Transport Server Setup (part 5) - Getting Edge Subscription Details, Synchronizing Edge Subscriptions
- Microsoft Exchange Server 2010 : Completing Transport Server Setup (part 4) - Creating an Edge Subscription
- Microsoft Exchange Server 2010 : Completing Transport Server Setup (part 3) - Enabling Anti-Spam Features
- Microsoft Exchange Server 2010 : Completing Transport Server Setup (part 2) - Configuring the Transport Dumpster , Configuring Shadow Redundancy
- Microsoft Exchange Server 2010 : Completing Transport Server Setup (part 1) - Configuring Transport Limits
- Advanced Windows 7 Programming : Working in the Background - DEVELOPING TRIGGER-START SERVICES (part 7)
- Advanced Windows 7 Programming : Working in the Background - DEVELOPING TRIGGER-START SERVICES (part 6)
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro