UIMenu in ViewController

Discussion in 'iOS Programming' started by ChristianJapan, Jul 17, 2013.

  1. ChristianJapan macrumors 601

    ChristianJapan

    Joined:
    May 10, 2010
    Location:
    日本
    #1
    I would like asking for advise to solve a little issue I have with a pop-up menu in a UICollectionViewController.

    Inside the VC I have an action linked to a gesture "long press". When I long press inside a cell of the controller the action get triggered and the menu will be shown.

    But: the menu is not modal/exclusive on the screen; it actually moves around while I keep the finger on the screen. A bit distracting.

    What I would like to have is that the menu become fixed and only disappear after a function is selected or clicked outside the menu once.

    Here a slightly changed piece of code I use.

    Code:
    - (IBAction)handleLongPress:(id)sender
    {
       UILongPressGestureRecognizer *lp = sender;
       
       CGPoint p = [lp locationInView:lp.view];
       
       NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];
       if (indexPath != nil)
       {
          [self becomeFirstResponder];
       
          MyUIMenuItem *f1 = [[MyUIMenuItem alloc] initWithTitle:@"f1" action:@selector(function1)];
          MyUIMenuItem *f2 = [[MyUIMenuItem alloc] initWithTitle:@"f2" action:@selector(function2)];
          MyUIMenuItem *f3 = [[MyUIMenuItem alloc] initWithTitle:@"f3" action:@selector(function3)];
          MyUIMenuItem *f4 = [[MyUIMenuItem alloc] initWithTitle:@"f4" action:@selector(function4)];
       
          UIMenuController * menu = [UIMenuController sharedMenuController];
       
          menu.menuItems = [NSArray arrayWithObjects: f1, f2, f3, f4, nil];
    
          [menu setTargetRect: CGRectMake(p.x, p.y, 0, 00) inView: lp.view.superview];
          [menu setArrowDirection:UIMenuControllerArrowUp];
          [menu setMenuVisible: YES animated: YES];
    
          [menu update];
       }
    }
    
     
  2. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #2
    You should look at the sender.state property and only perform the code when the state is UIGestureRecognizerStateBegan

    This will cause the logic to only be carried out when the gesture begins, instead of during the entire gesture.
     
  3. ChristianJapan thread starter macrumors 601

    ChristianJapan

    Joined:
    May 10, 2010
    Location:
    日本
    #3
    Thanks for the right hint. That plus the attachment to the sending view instead of the superview made the behaviour now as it should be.

    Thanks again; much appreciated :)

    Code:
    - (IBAction)handleLongPress:(id)sender
    {
       UILongPressGestureRecognizer *lp = sender;
       
       CGPoint p = [lp locationInView:lp.view];
       
    [B]   if (lp.state == UIGestureRecognizerStateBegan)
    [/B]   {
    
          NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];
          if (indexPath != nil)
          {
             [self becomeFirstResponder];
       
             MyUIMenuItem *f1 = [[MyUIMenuItem alloc] initWithTitle:@"f1" action:@selector(function1)];
             MyUIMenuItem *f2 = [[MyUIMenuItem alloc] initWithTitle:@"f2" action:@selector(function2)];
             MyUIMenuItem *f3 = [[MyUIMenuItem alloc] initWithTitle:@"f3" 
    action:@selector(function3)];
             MyUIMenuItem *f4 = [[MyUIMenuItem alloc] initWithTitle:@"f4" action:@selector(function4)];
       
             UIMenuController * menu = [UIMenuController sharedMenuController];
       
             menu.menuItems = [NSArray arrayWithObjects: f1, f2, f3, f4, nil];
    
             [menu setTargetRect: CGRectMake(p.x, p.y, 0, 00) inView: [B]lp.view[/B]];
             [menu setArrowDirection:UIMenuControllerArrowUp];
             [menu setMenuVisible: YES animated: YES];
    
             [menu update];
          }
       }
    }
     

Share This Page