ios – How can I show the UIMenu programmatically?

0
68


I have assigned two UILongPressGestureRecognizer objects to a UIButton.

First one, is named longPressGestureRecognizer and has minimumPressDuration = 0.5

Second one, is named prolongedPressGestureRecognizer and has minimumPressDuration = 1.5

self.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];

        self.longPressGestureRecognizer.delegate = self;
        
        self.longPressGestureRecognizer.minimumPressDuration = 0.5;
        
        self.longPressGestureRecognizer.numberOfTouchesRequired = 1;
        
        self.longPressGestureRecognizer.numberOfTapsRequired = 0;
        
        self.longPressGestureRecognizer.allowableMovement = 10.0;
        
        [self.customButton addGestureRecognizer:self.longPressGestureRecognizer];
     
        self.prolongedPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(prolongedPress:)];
        
        self.prolongedPressGestureRecognizer.delegate = self;
        
        self.prolongedPressGestureRecognizer.minimumPressDuration = 1.5;
        
        self.prolongedPressGestureRecognizer.numberOfTouchesRequired = 1;
        
        self.prolongedPressGestureRecognizer.numberOfTapsRequired = 0;
        
        self.prolongedPressGestureRecognizer.allowableMovement = 10.0;
        
        [self.customButton addGestureRecognizer:self.prolongedPressGestureRecognizer];

Scenarios:

When the first one fires I’d like for something to happen.

When the second one fires I’d like for the context menu to show.

Currently, I have no way to do this.

Solutions:

  1. Is it possible to delay the time it takes for the context menu to appear? I’m sure there’s a long press gesture recognizer internally that shows the menu. Can I modify this gesture recognizer?

2. Is it possible to show the menu programmatically?

        NSMutableArray* actions = [[NSMutableArray alloc] init];

        [actions addObject:[UIAction actionWithTitle:@"Edit"
                                               image:nil
                                          identifier:nil
                                             handler:^(__kindof UIAction* _Nonnull action) {
            
            // ...
        }]];

        UIMenu* menu =
        [UIMenu menuWithTitle:@""
                     children:actions];
        
        self.customButton.menu = menu;