1

I would like to use the same look and feel as the menu bar when clicking on a uicollectionviewcell as looks here:

enter image description here

However, instead of cut I would like to write move

Is it possible? I saw some answers - but they all asked to implement my own UIActionSheet - but I would like it to look like a menu and not an action sheet

Is it possible?

Dejell
  • 13,947
  • 40
  • 146
  • 229

2 Answers2

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];


    }
}
karthika
  • 4,085
  • 3
  • 21
  • 23
  • where should I put the UILongPressureGesture? – Dejell Sep 27 '13 at 11:30
  • In cellForItemAtIndexPath, you created cell right. here you add gesture for cell. – karthika Sep 27 '13 at 11:33
  • so actually self. collectionCell is the cell that I dequeue? [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath]; ? – Dejell Sep 27 '13 at 11:37
  • yes correct. Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath]; [cell addGestureRecognizer:longPressGesture]; – karthika Sep 27 '13 at 11:42
  • Thanks. I tried your code - the method showMenu is called, but the menu is not being displayed :( – Dejell Sep 27 '13 at 11:45
  • add these delegate methods, - (BOOL)canBecomeFirstResponder -(BOOL)canPerformAction:(SEL)action withSender:(id)sender – karthika Sep 27 '13 at 12:12
  • refer here for UIMenuItem, http://stackoverflow.com/questions/3112925/uimenucontroller-not-showing-up – karthika Sep 27 '13 at 12:18
  • I tried the code, but I get instead of one, many menu items which I didn't add/write. What is wrong? – Dejell Sep 27 '13 at 12:59
0

In case anyone else needs:

here is the correct answer from @Nilz11:

Add to 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]];

Add delegate methods

-(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;
}

Add the methods to a custom UICollectionViewCell. For instance:

-(void)editMe:(UIMenuController *)menuController{
}
Community
  • 1
  • 1
Dejell
  • 13,947
  • 40
  • 146
  • 229