I am creating menu on longPressGesture of some view.
The code is as below.
- (void)showMenu:(UILongPressGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
[self becomeFirstResponder];
/*
Set up th menu.
*/
UIMenuItem *menuItem1 = [[UIMenuItem alloc] initWithTitle:@"Item 1" action:@selector(item1Selected:)];
UIMenuItem *menuItem2 = [[UIMenuItem alloc] initWithTitle:@"Item 2" action:@selector(item2Selected:)];
UIMenuController *menuController = [UIMenuController sharedMenuController];
[menuController setMenuItems:@[menuItem1,menuItem2]];
CGPoint location = [gestureRecognizer locationInView:[gestureRecognizer view]];
CGRect menuLocation = CGRectMake(location.x, location.y, 0, 0);
[menuController setTargetRect:menuLocation inView:[gestureRecognizer view]];
[menuController setMenuVisible:YES animated:YES];
}
}
- (void)item1Selected:(UIMenuController *)controller
{
NSLog("Item 1 Selected");
}
- (void)item2Selected:(UIMenuController *)controller
{
NSLog("Item 2 Selected");
}
// UIMenuController requires that we can become first responder or it won't display
- (BOOL)canBecomeFirstResponder
{
return YES;
}