iOS MenuController

How to add the system's menu in your gesture?

What you should do must same with this:

1) - (BOOL)canBecomeFirstResponder must return YES;

2) - (BOOL)canPerformAction:(SEL)action withSender:(id)sender what you want to show item should return YES;

3) Before you show the menu, you should call the method: [self becomeFirstResponder];

4) If you want to show the second menu, you should call the method "setMenuVisible:NO",otherwise the menu will not show.

This is the whole view controller .m file content, nothing content in the .h file.

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGesture:)];
    [self.view addGestureRecognizer:longGesture];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(copy:)) {
        return YES;
    }
    return NO;
}

- (void)longGesture:(UILongPressGestureRecognizer *)longGesture
{
    if (longGesture.state == UIGestureRecognizerStateBegan) {[self becomeFirstResponder];
        UIMenuController *menuController = [UIMenuController sharedMenuController];
        [menuController setTargetRect:CGRectMake(0, 0, 50, 50) inView:self.view];
        [menuController setMenuVisible:YES animated:YES];
    }
    
}

- (void)copy:(id)sneder
{
    
}

- (void)delete:(id)sender
{}

- (void)move:(id)sender
{}

@end

Reference Link:http://www.cocoachina.com/bbs/read.php?tid=75860&keyword=menucontroller

Thanks,

Blues

你可能感兴趣的:(iOS MenuController)