Friday, January 11, 2013

Getting the duration of a touch on iPhone

Since iOS version 5, gesture recognizers have been added to the list of controls in the Interface Builder library. But sometimes we want more information about a touch than these handlers can easily provide. Today, I’ll show you a simple way to get the exact duration of a touch on the screen. So let’s get started!

Start Xcode, select “Create a new Xcode project,” and select the Single View Application template. Click Next, name the project TouchDuration, and select options as shown here:

Click Next, choose a location to save the project, and click Create.

Open ViewController.xib, and drag two UILabels from the library to the view. Set the text of the first label to “Touch the screen” and delete all text from the second label. The view should look something like this:

Open ViewController.h and make the following changes:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (nonatomic, strong) IBOutlet UILabel *lbDisplay;
@property (nonatomic, strong) IBOutlet UILabel *lbTime;
@property (nonatomic, strong) NSDate *startTime;

@end

We’ve added two outlet properties for the labels, and an NSDate property named startTime. Return to ViewController.xib, and wire up the two outlets to their corresponding labels:

Open ViewController.m and alter the code as shown :

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize lbDisplay, lbTime;
@synthesize startTime;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([touches count] == 1) {
        self.startTime = [NSDate date];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([touches count] == 1) {
        NSDate *now = [NSDate date];
        NSTimeInterval deltaT = [now timeIntervalSinceDate:self.startTime];
        if (deltaT > 0.5) {
            self.lbDisplay.text = @"Long Touch (> .5 seconds)";
        } else {
            self.lbDisplay.text = @"Short Touch (<= .5 seconds)";
        }
        self.lbTime.text = [NSString stringWithFormat:@"Exact time: %f seconds", deltaT];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    self.lbDisplay = nil;
    self.lbTime = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

After synthesizing the properties, we’ve added two methods: touchesBegan: withEvent: and touchesEnded: withEvent:. In touchesBegan, after verifying that only one finger is involved in the touch, we simply obtain the current time in self.startTime. In touchesEnded, we again get the current time, then calculate the time difference between “now” and the startTime. The timeIntervalSinceDate: method of an NSDate object returns a float: this is the difference between two NSDates expressed in seconds. (NSTimeInterval is simply an alias for float.)

In viewDidUnload, we release both of the label objects, since they were retained by the viewController (they are “strong” properties).

Now run the application. As expected, not only does it inform the user whether the touch was “long” or “short,” but also exactly how long the touch lasted.

In most cases, we can use the new gesture recognizers provided for us in Interface Builder. But if we need to get the exact duration of a touch, the procedure just discussed provides an easy way.


Source : edumobile[dot]org

0 comments:

Post a Comment