1

在单击 uicollectionviewcell 时,我想使用与菜单栏相同的外观,如下所示:

在此处输入图像描述

但是,我想写 move 而不是 cut

可能吗?我看到了一些答案——但他们都要求实现我自己的 UIActionSheet——但我希望它看起来像一个菜单而不是一个操作表

可能吗?

4

2 回答 2

1
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showMenu:)];
    longPressGesture.minimumPressDuration=0.4;
    [longPressGesture setDelegate:self];
    [self.collectionCell addGestureRecognizer:longPressGesture];

- (void) showMenu:(UILongPressGestureRecognizer *)gestureRecognizer
{

    if ([gestureRecognizer state] == UIGestureRecognizerStateEnded) {

          UIMenuController *menuController = [UIMenuController sharedMenuController];
            UIMenuItem *menuItem1 = [[UIMenuItem alloc] initWithTitle:@"Cut" action:@selector(copyAction:)];
            UIMenuItem *menuItem3 = [[UIMenuItem alloc] initWithTitle:@"Paste" action:@selector(pasteAction)];
            CGPoint location = [gestureRecognizer locationInView:[gestureRecognizer view]];

            [menuController setMenuItems:[NSArray arrayWithObjects: menuItem1, menuItem1,nil]];
            [menuController setTargetRect:CGRectMake(location.x, location.y, 0, 0) inView:[gestureRecognizer view]];
            [menuController setMenuVisible:YES animated:YES];

               [self becomeFirstResponder];


    }
}
于 2013-09-27T11:28:12.057 回答
0

如果其他人需要:

这是@Nilz11 的正确答案

添加到 ViewDidLoad

   UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Edit" action:@selector(editMe:)];
    UIMenuItem *menuItem2 = [[UIMenuItem alloc] initWithTitle:@"Move" action:@selector(moveMe:)];
    UIMenuItem *menuItem3 = [[UIMenuItem alloc] initWithTitle:@"DeleteMe" action:@selector(deletePlate:)];
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:menuItem,menuItem2,menuItem3, nil]];

添加委托方法

-(BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    if (action == @selector(editMe:)  || action == @selector(moveMe:) || action == @selector(deleteMe:))
        return YES;
    return NO;
}

-(BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

将方法添加到自定义 UICollectionViewCell。例如:

-(void)editMe:(UIMenuController *)menuController{
}
于 2013-09-30T14:06:39.423 回答