Graphics Programs Reference
In-Depth Information
UIMenuController
When the user selects a line, we want a menu to appear right where the user tapped that of-
fers the option to delete that line. There is a built-in class for providing this sort of menu
called UIMenuController . A menu controller has a list of menu items 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 view it is being presented in).
Figure 20.3 A UIMenuController
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 vis-
ible. Do this in TouchDrawView.m 's tap: method if the user has tapped on a line. If
the user tapped somewhere that is not near a line, the currently selected line will be
deselected, and the menu controller will hide.
- (void)tap:(UIGestureRecognizer *)gr
{
NSLog(@"Recognized tap");
CGPoint point = [gr locationInView:self];
[self setSelectedLine:[self lineAtPoint:point]];
[linesInProcess removeAllObjects];
if ([self selectedLine]) {
// We'll talk about this shortly
[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 setMenuItems:[NSArray arrayWithObject: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 {
// Hide the menu if no line is selected
[[UIMenuController sharedMenuController] setMenuVisible:NO animated:YES];
}
[self setNeedsDisplay];
}
Search WWH ::




Custom Search