UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(pressClick:)];
[self.view addGestureRecognizer:longPress];
self.view.userInteractionEnabled = YES;
{
[self becomeFirstResponder];
[self popMemu];
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
// 自定义方法时才显示对就选项菜单,即屏蔽系统选项菜单
if (action == @selector(copyClick) || action == @selector(deleteClick) || action == @selector(moveClick))
{
return YES;
}
return NO;
}
- (void)popMemu
{
// @selector()括号中为该按钮触发的方法,该方法必须在UIVIewContrller中进行声明,就是投向的view所绑定的viewController类中必须实现这个方法
UIMenuItem *menuItem_1 = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copyClick)];
UIMenuItem *menuItem_2 = [[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(deleteClick)];
UIMenuItem *menuItem_3 = [[UIMenuItem alloc] initWithTitle:@"移动" action:@selector(moveClick)];
UIMenuController *menuController = [UIMenuController sharedMenuController];
menuController.menuItems = [NSArray arrayWithObjects:menuItem_1, menuItem_2, menuItem_3, nil];
// touchpos_x, touchpos_y分别为长按那点的x和y坐标 self.view为将要展示弹出框的视图
CGFloat touchpos_x = 40.0;
CGFloat touchpos_y = 100.0;
[menuController setTargetRect:CGRectMake(touchpos_x, touchpos_y, 100.0, 40.0) inView:self.view];
[menuController setMenuVisible:YES animated:YES];
}
- (void)copyClick
{
NSLog(@"copy");
}
- (void)deleteClick
{
NSLog(@"delete");
}
- (void)moveClick
{
NSLog(@"move");
}