Friday, October 7, 2022
HomeiOS Developmentios - How can I present the UIMenu programmatically?

ios – How can I present the UIMenu programmatically?


I’ve assigned two UILongPressGestureRecognizer objects to a UIButton.

First one, is called longPressGestureRecognizer and has minimumPressDuration = 0.5

Second one, is called prolongedPressGestureRecognizer and has minimumPressDuration = 1.5

self.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self motion:@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 motion:@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];

Eventualities:

When the primary one fires I might like for one thing to occur.

When the second fires I might like for the context menu to indicate.

Presently, I’ve no manner to do that.

Options:

  1. Is it doable to delay the time it takes for the context menu to look? I am certain there is a lengthy press gesture recognizer internally that reveals the menu. Can I modify this gesture recognizer?

2. Is it doable to indicate 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;

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments