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

618

CHAPTER 17: Taps, Touches, and Gestures

Implementing Multiple Swipes

In the Swipes application, we worried about only single-finger swipes, so we just grabbed any object out of the touches set to figure out where the user’s finger was during the swipe. This approach is fine if you’re interested in only single-finger swipes, the most common type of swipe used.

But what if you want to handle twoor three-finger swipes? In previous versions of this book, we dedicated about 50 lines of code, and a fair amount of explanation, to achieving this by tracking multiple UITouch instances across multiple touch events. Now that we have gesture recognizers, this is a solved problem. A UISwipeGestureRecognizer can be configured to recognize any number of simultaneous touches. By default, each instance expects a single finger, but you can configure it to look for any number of fingers pressing the screen at once. Each instance responds only to the exact number of touches you specify, so what we’ll do is create a whole bunch of gesture recognizers in a loop.

Make a copy of your Swipes project folder.

Edit BIDViewController.m and modify the viewDidLoad method, replacing it with the one shown here:

- (void)viewDidLoad

{

[super viewDidLoad];

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

for (NSUInteger touchCount = 1; touchCount <= 5; touchCount++) { UISwipeGestureRecognizer *vertical;

vertical = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(reportVerticalSwipe:)];

vertical.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown;

vertical.numberOfTouchesRequired = touchCount; [self.view addGestureRecognizer:vertical];

UISwipeGestureRecognizer *horizontal;

horizontal = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(reportHorizontalSwipe:)];

horizontal.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;

horizontal.numberOfTouchesRequired = touchCount; [self.view addGestureRecognizer:horizontal];

}

}

Note that in a real application, you might want different numbers of fingers swiping across the screen to trigger different behaviors. You can easily do that using gesture recognizers, simply by having each of them call a different action method.

Now, all we need to do is change the logging, by adding a method that gives us a handy description of the number of touches, and using it in the reporting methods as shown

www.it-ebooks.info

CHAPTER 17: Taps, Touches, and Gestures

619

here. Add this method toward the bottom of the BIDViewController class, just above the two swipe-reporting methods:

- (NSString *)descriptionForTouchCount:(NSUInteger)touchCount { switch (touchCount) {

case 2:

return @"Double "; case 3:

return @"Triple "; case 4:

return @"Quadruple "; case 5:

return @"Quintuple "; default:

return @"";

}

}

Next, modify the two swipe-reporting methods as shown:

- (void)reportHorizontalSwipe:(UIGestureRecognizer *)recognizer { label.text = @"Horizontal swipe detected";

label.text = [NSString stringWithFormat:@"%@Horizontal swipe detected", [self descriptionForTouchCount:[recognizer numberOfTouches]]];

[self performSelector:@selector(eraseText) withObject:nil afterDelay:2];

}

- (void)reportVerticalSwipe:(UIGestureRecognizer *)recognizer { label.text = @"Vertical swipe detected";

label.text = [NSString stringWithFormat:@"%@Vertical swipe detected", [self descriptionForTouchCount:[recognizer numberOfTouches]]];;

[self performSelector:@selector(eraseText) withObject:nil afterDelay:2];

}

Compile and run the app. You should be able to trigger doubleand triple-swipes in both directions, and still be able to trigger single-swipes. If you have small fingers, you might even be able to trigger a quadrupleor quintuple-swipe.

TIP: In the simulator, if you hold down the option key, a pair of dots, representing a pair of fingers, will appear. Get them close together, then hold down the shift key, and the dots will stay in the same position relative to each other, allowing you to move the pair of fingers around the

screen. Now click and drag down the screen to simulate a double-swipe. Cool!

With a multiple-finger swipe, one thing to be careful of is that your fingers aren’t too close to each other. If two fingers are very close to each other, they may register as only a single touch. Because of this, you shouldn’t rely on quadrupleor quintuple-swipes for any important gestures, because many people will have fingers that are too big to do those swipes effectively.

www.it-ebooks.info

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