Difference between revisions of "An iPhone iOS 5 Gesture Recognition Tutorial"

From Techotopia
Jump to: navigation, search
m (Text replacement - "<table border="0" cellspacing="0">" to "<table border="0" cellspacing="0" width="100%">")
 
(6 intermediate revisions by the same user not shown)
Line 8: Line 8:
  
  
<google>BUY_IOS5</google>
+
<htmlet>ios9_upgrade</htmlet>
  
  
Line 156: Line 156:
  
  
<google>BUY_IOS5</google>
+
<htmlet>ios9_upgrade</htmlet>
  
  
 +
 +
<htmlet>ezoicbottom</htmlet>
 
<hr>
 
<hr>
 
<table border="0" cellspacing="0" width="100%">
 
<table border="0" cellspacing="0" width="100%">

Latest revision as of 19:55, 27 October 2016

PreviousTable of ContentsNext
Identifying iPhone Gestures using iOS 5 Gesture RecognizersDrawing iOS 5 iPhone 2D Graphics with Quartz


Learn SwiftUI and take your iOS Development to the Next Level
SwiftUI Essentials – iOS 16 Edition book is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book


Having covered the theory of gesture recognition on the iPhone in the chapter entitled Identifying iPhone Gestures using iOS 5 Gesture Recognizers, the purpose of this chapter is to work through an example application intended to demonstrate the use of the various UIGestureRecognizer subclasses.

The application created in this chapter will configure recognizers to detect a number of different gestures on the iPhone display and update a status label with information about each recognized gesture.


Contents


Creating the Gesture Recognition Project

Begin by invoking Xcode and creating a new iOS iPhone application project using the Single View Application template and name the project and class prefix recognizer.

Configuring the Label Outlet

The only component that will be present on our UIView object will be the label used to notify the user of the type of gesture detected. Since the text displayed on this label will need to be updated from within the application code it will need to be connected to an outlet. Within the Xcode project navigator panel, select the recognizerViewController.h file and modify it as follows:

#import <UIKit/UIKit.h>

@interface recognizerViewController : UIViewController
{
    UILabel *statusLabel;
}
@property (strong, nonatomic) IBOutlet UILabel *statusLabel;
@end

Next, edit the recognizerViewController.m file and add the @synthesizer directive for the new outlet and code to release the allocated memory:

#import "recognizerViewController.h"

@implementation recognizerViewController
@synthesize statusLabel;
.
.
.
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.statusLabel = nil;
}
.
.
@end

Designing the User Interface

Select the recognizerViewController.xib file to display the interface builder panel. In addition to the UIView created for us by Xcode when the project was created the only user interface object that needs to be added is the status label. Display the Object library (View -> Utilities -> Show Object Library) and drag a Label object from the library to the center of the view. Once positioned, stretch the label to the outer edges of the view until the blue dotted lines representing the recommended margins appear and then modify the label properties to center the label text.

Ctrl+click on the File’s Owner object, drag the blue line to the label object and release. From the resulting menu select the statusLabel outlet.

Configuring the Gesture Recognizers

The application code will need to configure gesture recognizers for taps, swipes, rotations and pinches. Since these recognizers will need to be attached to the view object the ideal place to create them is in the viewDidLoad method of our recognizerViewController class, so select the recognizerViewController.m file and modify this method so that it reads as follows:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITapGestureRecognizer *doubleTap = 
       [[UITapGestureRecognizer alloc]
       initWithTarget:self 
       action:@selector(tapDetected:)];
    doubleTap.numberOfTapsRequired = 2;
    [self.view addGestureRecognizer:doubleTap];

    UIPinchGestureRecognizer *pinchRecognizer = 
        [[UIPinchGestureRecognizer alloc]
        initWithTarget:self 
        action:@selector(pinchDetected:)];
    [self.view addGestureRecognizer:pinchRecognizer];

    UIRotationGestureRecognizer *rotationRecognizer =
        [[UIRotationGestureRecognizer alloc]
        initWithTarget:self 
        action:@selector(rotationDetected:)];
    [self.view addGestureRecognizer:rotationRecognizer];

    UISwipeGestureRecognizer *swipeRecognizer = 
       [[UISwipeGestureRecognizer alloc]
       initWithTarget:self 
       action:@selector(swipeDetected:)];
    swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipeRecognizer];

    UILongPressGestureRecognizer *longPressRecognizer = 
         [[UILongPressGestureRecognizer alloc]
         initWithTarget:self 
         action:@selector(longPressDetected:)];
    longPressRecognizer.minimumPressDuration = 3;
    longPressRecognizer.numberOfTouchesRequired = 1;
    [self.view addGestureRecognizer:longPressRecognizer];
} 

Adding the Action Methods

Having configured the gesture recognizers, the next step is to write the action methods that will be called by each recognizer when the corresponding gesture is detected. These methods will also reside in the recognizerViewController.m file and will update the status label with information about the detected gesture:

- (IBAction)longPressDetected:(UIGestureRecognizer *)sender {
    statusLabel.text = @"Long Press";
}

- (IBAction)swipeDetected:(UIGestureRecognizer *)sender {
    statusLabel.text = @"Right Swipe";
}

- (IBAction)tapDetected:(UIGestureRecognizer *)sender {
    statusLabel.text = @"Double Tap";
}

- (IBAction)pinchDetected:(UIGestureRecognizer *)sender {

    CGFloat scale = 
       [(UIPinchGestureRecognizer *)sender scale];
    CGFloat velocity = 
       [(UIPinchGestureRecognizer *)sender velocity];

    NSString *resultString = [[NSString alloc] initWithFormat:
         @"Pinch - scale = %f, velocity = %f",
         scale, velocity];
    statusLabel.text = resultString;
}


- (IBAction)rotationDetected:(UIGestureRecognizer *)sender {
    CGFloat radians = 
          [(UIRotationGestureRecognizer *)sender rotation];
    CGFloat velocity = 
          [(UIRotationGestureRecognizer *)sender velocity];

    NSString *resultString = [[NSString alloc] initWithFormat:
              @"Rotation - Radians = %f, velocity = %f",
              radians, velocity];
    statusLabel.text = resultString;
}

Testing the Gesture Recognition Application

The final step is to build and run the application. In order to fully test the pinching and rotation recognition it will be necessary to run the application on a physical device (since it is not possible to emulate two simultaneous touches within the iOS Simulator environment). Assuming a provisioned device is attached (see Testing iOS 5 Apps on the iPhone – Developer Certificates and Provisioning Profiles for more details) simply click on the Xcode Run button. Once the application loads on the device, perform the appropriate gestures on the display and watch the status label update accordingly.


Learn SwiftUI and take your iOS Development to the Next Level
SwiftUI Essentials – iOS 16 Edition book is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book



PreviousTable of ContentsNext
Identifying iPhone Gestures using iOS 5 Gesture RecognizersDrawing iOS 5 iPhone 2D Graphics with Quartz