首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何以编程方式显示UIMenu?

如何以编程方式显示UIMenu?
EN

Stack Overflow用户
提问于 2022-09-23 19:37:18
回答 1查看 142关注 0票数 0

我已经为一个UILongPressGestureRecognizer分配了两个UIButton对象。

第一个名为longPressGestureRecognizer,具有minimumPressDuration = 0.5

第二个,命名为prolongedPressGestureRecognizer,具有minimumPressDuration = 1.5

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

代码语言:javascript
复制
        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];

设想方案:

当第一次开火的时候,我想让事情发生。

当第二次触发时,我希望上下文菜单显示。

目前,我没有办法这样做。

解决办法:

  1. 是否可以延迟上下文菜单出现所需的时间?我确信有一个长新闻手势识别器内部显示菜单。我可以修改这个手势识别器吗?

2.是否可以以编程方式显示菜单?

代码语言:javascript
复制
        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;
EN

回答 1

Stack Overflow用户

发布于 2022-09-23 20:11:06

你可能需要这样的东西。self.layoutButton将菜单显示为主要操作。意思是普通的水龙头。不是一个主要的动作是一个很长的敲打。

代码语言:javascript
复制
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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73832252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档