Chapter 13 UIGestureRecognizer and UIMenuController
1. A UIGestureRecognizer intercepts touches that are on their way to being handled by a view. When it recognizes a particular gesture, it sends a message to the object of your choice. There are several types of gesture recognizers built into the SDK.
2. A UIMenuController has a list of UIMenuItem objects and is presented in an existing view. Each item has a title (what shows up in the menu) and an action (the message it sends the first responder of the window).
3. There is only one UIMenuController per application. When you wish to present this instance, you fill it with menu items, give it a rectangle to present from, and set it to be visible.
-(void)tap:(UIGestureRecognizer*)gr
{
NSLog(@"Recognized tap");
CGPoint point = [gr locationInView:self];
self.selectedLine = [self lineAtPoint:point];
if(self.selectedLine)
{
// Make the target of menu item action messages
[self becomeFirstResponder];
// Grab the menu controller
UIMenuController *menu = [UIMenuController sharedMenuController];
// Create a new "delete" UIMenuItem
UIMenuItem *deleteItem = [[UIMenuItem alloc] initWithTitle:@"delete" action:@selector(deleteLine:)];
menu.menuItems = @[deleteItem];
// Tell the menu where it should come from and show it
[menu setTargetRect:CGRectMake(point.x, point.y, 2, 2) inView:self];
[menu setMenuVisible:YES animated:YES];
}
else
{
[[UIMenuController sharedMenuController] setMenuVisible:NO animated:YES];
}
[self setNeedsDisplay];
}
4. For a menu controller to appear, a view that responds to at least one action message in the UIMenuController’s menu items must be the first responder of the window - this is why you sent the message becomeFirstResponder to the view before setting up the menu controller. If you have a custom view class that needs to become the first responder, you must override canBecomeFirstResponder.
-(BOOL)canBecomeFirstResponder
{return YES;}
5. Normally, a gesture recognizer does not share the touches it intercepts. Once it has recognized its gesture, it “eat” that touch, and no other recognizer gets a chance to handle it.
6. gestureRecognizer: shouledRecognizeSimultaneouslyWithGestureRecognizer. A gesture recognizer will send this message to its delegate when it recognizes its gesture but realizes that another gesture recognizer has recognized its gesture, too. If this method returns YES, the recognizer will share its touches with other gesture recognizers.
7. A pan gesture recognizer supports the changed state. When a finger starts to move, the pan recognizer enters the began state and sends a message to its target. While the finger moves around the screen, the recognizer transitions to the changed state and sends its action message to its target repeatedly. Finally, when the finger leaves the screen, the recognizer’s state is set to ended, and the final message is delivered to the target.
8. Every UIGestureRecognizer has a property cancelsTouchesInView, by default, this property is YES. This means that gesture recognizer will eat any touch it recognizes so that the view will not have a chance to handle it via the traditional UIResponder methods, like touchesBegan:withEvent:. When you set cancelsTouchesInView to NO, touches that the gesture recognizer recognizes also get delivered to the view via the UIResponder methods. This allows both the recognizer and the view’s UIResponder methods to handle the same touches.
9. Overall, there are seven states a recognizer can enter:
UIGestureRecognizerStatePossible
UIGestureRecognizerStateFailed
UIGestureRecognizerStateBegan
UIGestureRecognizerStateCancelled
UIGestureRecognizerStateChanged
UIGestureRecognizerStateRecognized
UIGestureRecognizerStateEnded
Most of the time, a recognizer will stay in the possible state. When a recognizer recognized its gesture, it goes into the began state. If the gesture is something that can continue, like a pan, it will go into and stay in the changed state until it ends. When any of its properties change, it sends another message to its target. When the gesture ends (typically when the user lifts the finger), it endures the ended state.