Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning iOS5 Development.pdf
Скачиваний:
7
Добавлен:
09.05.2015
Размер:
15.6 Mб
Скачать

CHAPTER 17: Taps, Touches, and Gestures

625

Note that we don’t need to explicitly configure every gesture to be dependent on the failure of each of the higher-tap-numbered gestures. That multiple dependency comes about naturally as a result of the chain of failure established in our code. Since singleTap requires the failure of doubleTap, doubleTap requires the failure of tripleTap, and tripleTap requires the failure of quadrupleTap, by extension, singleTap requires that all of the others fail.

Compile and run the app, and whether you single-, double-, triple-, or quadruple-tap, you should see only one label displayed.

Detecting Pinches

Another common gesture is the two-finger pinch. It’s used in a number of applications, including Mobile Safari, Mail, and Photos, to let you zoom in (if you pinch apart) or zoom out (if you pinch together).

Detecting pinches is really easy, thanks to UIPinchGestureRecognizer. This one is referred to as a continuous gesture recognizer, because it calls its action method over and over again during the pinch. While the gesture is underway, the recognizer goes through a number of states. The only one we want to watch for is UIGestureRecognizerStateBegan, which is the state that the recognizer is in when it first calls the action method after detecting that a pinch is happening. At that moment, the pinch gesture recognizer’s scale property is always set to 1.0; for the rest of the gesture, that number goes up and down, relative to how far the user’s fingers move from the start. We’re going to use the scale value to resize the text in a label.

Create a new project in Xcode, again using the Single View Application template, and call this one PinchMe. The PinchMe application will need only a single outlet for a label, but it also needs a property to hold the size of the label’s font at the start of the pinch. Expand the PinchMe folder, single-click BIDViewController.h, and make the following changes:

#import <UIKit/UIKit.h>

@interface BIDViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *label; @property (assign, nonatomic) CGFloat initialFontSize;

@end

Now that we have our outlet, edit BIDViewController.xib. In Interface Builder, make sure the view is displayed in its editing window, and drag a single label into the view, aligning it with the upper-left blue guidelines. Grab the lower-right corner of the label and resize it so it hits the lower-right blue guidelines.

Unlike some other examples we’ve shown, we need to have a bit of text in the label so there’s something to see. Double-click the label and change the text to a single capital letter X. This letter is what we’ll be zooming in and out on. Set the label’s alignment to centered. Next, control-drag from the File’s Owner icon to the label, and connect it to the label outlet.

www.it-ebooks.info

626

CHAPTER 17: Taps, Touches, and Gestures

Save the nib. Now, bounce over to BIDViewController.m, and add the following code at the top of the file:

#import "BIDViewController.h"

@implementation BIDViewController

@synthesize label; @synthesize initialFontSize;

.

.

.

Clean up our outlet in the viewDidUnload method:

- (void)viewDidUnload

{

[super viewDidUnload];

//Release any retained subviews of the main view.

//e.g. self.myOutlet = nil;

self.label = nil;

}

Then add the following code to the viewDidLoad method:

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(doPinch:)];

[self.view addGestureRecognizer:pinch];

}

And add the following method at the end of the file:

.

.

.

- (void)doPinch:(UIPinchGestureRecognizer *)pinch {

if (pinch.state == UIGestureRecognizerStateBegan) { initialFontSize = label.font.pointSize;

} else {

label.font = [label.font fontWithSize:initialFontSize * pinch.scale];

}

}

@end

In viewDidLoad, we set up a pinch gesture recognizer and tell it to notify us via the doPinch: method when pinching is occurring. Inside doPinch:, we look at the pinch’s state to see if it’s just starting, and if so, we store the current font size for later use. Otherwise, if the pinch is already in progress, we use the stored initial font size and the current pinch scale to calculate a new font size.

And that’s all there is to pinch detection. Compile and run the app to give it a try. As you do some pinching, you’ll see the text change size in response (see Figure 17–5). If you’re on the simulator, remember that you can simulate a pinch by holding down the option key and clicking and dragging in the simulator window using your mouse.

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]