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

606

CHAPTER 17: Taps, Touches, and Gestures

table view cell probably needs to look at that event to see if it contains a swipe gesture. Most table view cells don’t respond to gestures, however. If they don’t respond, the event proceeds up to the table view, and then up the rest of the responder chain until something responds to that event or it reaches the end of the line.

Forwarding an Event: Keeping the Responder Chain Alive

Let’s take a step back to that table view cell in the Mail application. We don’t know the internal details of Apple’s Mail application, but let’s assume, for the nonce, that the table view cell handles the delete swipe and only the delete swipe. That table view cell must implement the methods related to receiving touch events (discussed shortly) so that it can check to see if that event contained a swipe gesture. If the event contains a swipe, then the table view cell takes an action, and that’s that; the event goes no further.

If the event doesn’t contain a swipe gesture, the table view cell is responsible for forwarding that event manually to the next object in the responder chain. If it doesn’t do its forwarding job, the table and other objects up the chain will never get a chance to respond, and the application may not function as the user expects. That table view cell could prevent other views from recognizing a gesture.

Whenever you respond to a touch event, you need to keep in mind that your code doesn’t work in a vacuum. If an object intercepts an event that it doesn’t handle, it needs to pass it along manually, by calling the same method on the next responder. Here’s a bit of fictional code:

-(void)respondToFictionalEvent:(UIEvent *)event { if (someCondition)

[self handleEvent:event];

else

[self.nextResponder respondToFictionalEvent:event];

}

Notice how we call the same method on the next responder. That’s how to be a good responder-chain citizen. Fortunately, most of the time, methods that respond to an event also consume the event, but it’s important to know that if that’s not the case, you need to make sure the event is pushed back into the responder chain.

The Multitouch Architecture

Now that you know a little about the responder chain, let’s look at the process of handling gestures. As we’ve indicated, gestures are passed along the responder chain, embedded in events. This means that the code to handle any kind of interaction with the multitouch screen needs to be contained in an object in the responder chain. Generally, that means we can choose to either embed that code in a subclass of UIView or embed the code in a UIViewController.

So does this code belong in the view or in the view controller?

www.it-ebooks.info

CHAPTER 17: Taps, Touches, and Gestures

607

If the view needs to do something to itself based on the user’s touches, the code probably belongs in the class that defines that view. For example, many control classes, such as UISwitch and UISlider, respond to touch-related events. A UISwitch might want to turn itself on or off based on a touch. The folks who created the UISwitch class embedded gesture-handling code in the class so the UISwitch can respond to a touch.

Often, however, when the gesture being processed affects more than the object being touched, the gesture code really belongs in the view’s controller class. For example, if the user makes a gesture touching one row that indicates that all rows should be deleted, the gesture should be handled by code in the view controller. The way you respond to touches and gestures in both situations is exactly the same, regardless of the class to which the code belongs.

The Four Touch Notification Methods

Four methods are used to notify a responder about touches. When the user first touches the screen, the system looks for a responder that has a method called touchesBegan:withEvent:. To find out when the user first begins a gesture or taps the screen, implement this method in your view or your view controller. Here’s an example of what that method might look like:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSUInteger numTaps = [[touches anyObject] tapCount]; NSUInteger numTouches = [touches count];

// Do something here.

}

This method, and each of the touch-related methods, is passed an NSSet instance called touches and an instance of UIEvent. You can determine the number of fingers currently pressed against the screen by getting a count of the objects in touches. Every object in touches is a UITouch event that represents one finger touching the screen. If this touch is part of a series of taps, you can find out the tap count by asking any of the UITouch objects. In the preceding example, a numTaps value of 2 tells you that the screen was tapped twice in quick succession, while a numTouches value of 2 tells you the user tapped the screen with two fingers at once. If both have a value of 2, then the user double-tapped with two fingers.

All of the objects in touches may not be relevant to the view or view controller where you’ve implemented this method. A table view cell, for example, probably doesn’t care about touches that are in other rows or that are in the navigation bar. You can get a subset of touches that has only those touches that fall within a particular view from the event, like so:

NSSet *myTouches = [event touchesForView:self.view];

Every UITouch represents a different finger, and each finger is located at a different position on the screen. You can find out the position of a specific finger using the UITouch object. It will even translate the point into the view’s local coordinate system if you ask it to, like this:

www.it-ebooks.info

608

CHAPTER 17: Taps, Touches, and Gestures

CGPoint point = [touch locationInView:self];

You can get notified while the user is moving fingers across the screen by implementing touchesMoved:withEvent:. This method is called multiple times during a long drag, and each time it is called, you will get another set of touches and another event. In addition to being able to find out each finger’s current position from the UITouch objects, you can also discover the previous location of that touch, which is the finger’s position the last time either touchesMoved:withEvent: or touchesBegan:withEvent: was called.

When the user’s fingers are removed from the screen, another event, touchesEnded:withEvent:, is invoked. When this method is called, you know that the user is finished with a gesture.

There’s one final touch-related method that responders might implement. It’s called touchesCancelled:withEvent:, and it is called if the user is in the middle of a gesture when something happens to interrupt it, like the phone ringing. This is where you can do any cleanup you might need so you can start fresh with a new gesture. When this method is called, touchesEnded:withEvent: will not be called for the current gesture.

OK, enough theory—let’s see some of this in action.

The TouchExplorer Application

We’re going to build a little application that will give you a better feel for when the four touch-related responder methods are called. In Xcode, create a new project using the

Single View Application template. Enter TouchExplorer as the Product Name, and select iPhone for Device Family.

TouchExplorer will print messages to the screen, containing the touch and tap count, every time a touch-related method is called (see Figure 17–1).

www.it-ebooks.info

CHAPTER 17: Taps, Touches, and Gestures

609

Figure 17–1. The TouchExplorer application

NOTE: Although the applications in this chapter will run on the simulator, you won’t be able to see all of the available multitouch functionality unless you run them on a real iOS device. If you’ve been accepted into the iOS Developer Program, you have the ability to run the programs you write on your device of choice. The Apple web site does a great job of walking you through

the process of getting everything you need to prepare to connect Xcode to your device.

We need three labels for this application: one to indicate which method was last called, another to report the current tap count, and a third to report the number of touches. Single-click BIDViewController.h, and add three outlets and a method declaration as follows. The method will be used to update the labels from multiple places.

#import <UIKit/UIKit.h>

@interface BIDViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *messageLabel; @property (weak, nonatomic) IBOutlet UILabel *tapsLabel; @property (weak, nonatomic) IBOutlet UILabel *touchesLabel; - (void)updateLabelsFromTouches:(NSSet *)touches;

@end

www.it-ebooks.info

610

CHAPTER 17: Taps, Touches, and Gestures

Now, select BIDViewController.xib to edit the file. Click the View icon in the dock to edit the view if the view editor is not already open. Drag a label onto the view, using the blue guidelines to place the label toward the upper-left corner of the view. Use the resize handle to resize the label over to the right-hand blue guideline. Next, use the attribute inspector to set the label alignment to centered. Finally, hold down the option key and drag two more labels out from the original, spacing them one below the other, leaving you with three labels (see Figure 17–1).

Next, control-drag from the File’s Owner icon to each of the three labels, connecting the top one to the messageLabel outlet, the middle one to the tapsLabel outlet, and the last one to the touchesLabel outlet.

Feel free to play with the fonts and colors if you’re feeling a bit Picasso. When you’re finished placing the labels, double-click each one, and press the delete key to get rid of the text that’s in them.

Next, single-click the View icon in the nib dock and bring up the attributes inspector (see Figure 17–2). On the inspector, go to the View section and make sure that both User Interaction Enabled and Multiple Touch are checked. If Multiple Touch is not checked, your controller class’s touch methods will always receive one and only one touch, no matter how many fingers are actually touching the phone’s screen.

www.it-ebooks.info

CHAPTER 17: Taps, Touches, and Gestures

611

Figure 17–2. In the View attributes, make sure both User Interaction Enabled and Multiple Touch are checked.

When you’re finished, save the nib. Next, select BIDViewController.m, and add the following code at the beginning of the file:

#import "BIDViewController.h"

@implementation BIDViewController

@synthesize messageLabel; @synthesize tapsLabel; @synthesize touchesLabel;

- (void)updateLabelsFromTouches:(NSSet *)touches { NSUInteger numTaps = [[touches anyObject] tapCount]; NSString *tapsMessage = [[NSString alloc]

initWithFormat:@"%d taps detected", numTaps]; tapsLabel.text = tapsMessage;

NSUInteger numTouches = [touches count];

NSString *touchMsg = [[NSString alloc] initWithFormat: @"%d touches detected", numTouches];

touchesLabel.text = touchMsg;

}

.

www.it-ebooks.info

612CHAPTER 17: Taps, Touches, and Gestures

.

.

Then insert the following lines of code into the existing viewDidUnload methods:

- (void)viewDidUnload { [super viewDidUnload];

//Release any retained subviews of the main view.

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

self.messageLabel = nil; self.tapsLabel = nil; self.touchesLabel = nil;

}

And add the following new methods at the end of the file:

#pragma mark -

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { messageLabel.text = @"Touches Began";

[self updateLabelsFromTouches:touches];

}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ messageLabel.text = @"Touches Cancelled";

[self updateLabelsFromTouches:touches];

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { messageLabel.text = @"Touches Ended.";

[self updateLabelsFromTouches:touches];

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { messageLabel.text = @"Drag Detected";

[self updateLabelsFromTouches:touches];

}

@end

In this controller class, we implement all four of the touch-related methods we discussed earlier. Each one sets messageLabel so the user can see when each method is called. Next, all four of them call updateLabelsFromTouches: to update the other two labels. The updateLabelsFromTouches: method gets the tap count from one of the touches, figures out the number of touches by looking at the count of the touches set, and updates the labels with that information.

Compile and run the application. If you’re running in the simulator, try repeatedly clicking the screen to drive up the tap count, and try clicking and holding down the mouse button while dragging around the view to simulate a touch and drag. Note that a drag is not the same as a tap, so once you start your drag, the app will report zero taps.

You can emulate a two-finger pinch in the iOS simulator by holding down the option key while you click with the mouse and drag. You can also simulate two-finger swipes by first holding down the option key to simulate a pinch, then moving the mouse so the two dots representing virtual fingers are next to each other, and then holding down the shift

www.it-ebooks.info

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