Vulkan
Vulkan

Reputation: 1076

How can I show the UIMenu programmatically?

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;

Upvotes: 1

Views: 1513

Answers (1)

Cynichniy Bandera
Cynichniy Bandera

Reputation: 6103

You probably need something like this. self.layoutButton shows the menu as a primary action. It means normal tap. Not a primary action is a long tap.

NSMutableArray<UIAction *> *actions = [NSMutableArray array];
__weak __typeof__(self) blockSelf = self;
for (NSString *type in sortedKeys) {
        ZoneLayout *layout = [LayoutManager layoutByType:type];
        UIAction *action = [UIAction actionWithTitle:layout.title image:nil identifier:type
                                             handler:^(__kindof UIAction * _Nonnull action) {
                __strong __typeof__(blockSelf) strongSelf = blockSelf;
                if (strongSelf) {
                        strongSelf.layoutType = type;
                        [strongSelf setupZoneLayoutMenu];
                }
        }];
        if ([self.layoutType isEqualToString:type])
                action.state = UIMenuElementStateOn;
        else
                action.state = UIMenuElementStateOff;
        [actions addObject:action];
}
ZoneLayout *layout = [LayoutManager layoutByType:self.layoutType];
if (layout != nil)
        [self.layoutButton setTitle:layout.title forState:UIControlStateNormal];
else
        [self.layoutButton setTitle:@"-" forState:UIControlStateNormal];
self.layoutButton.menu = [UIMenu menuWithChildren:actions];
self.layoutButton.showsMenuAsPrimaryAction = YES;

Upvotes: 0

Related Questions