Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ChristianVirtual

macrumors 601
Original poster
May 10, 2010
4,122
282
日本
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];
   }
}
 
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.
 
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];
      }
   }
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.