美文网首页iOS 知识收集
UIMenus的简单使用

UIMenus的简单使用

作者: Gxdy | 来源:发表于2019-10-18 16:52 被阅读0次

    👉Demo

    1. 示例

    官方效果示例图 UI View TableView & CollectionView

    2. UIMenus 相关 Class简单介绍

    以下Class都是在iOS 13.0新增的,所以只能在iOS 13.0及以上版本中使用

    • UIContextMenuInteraction 用于显示内容相关操作的交互对象
    • UIContextMenuInteractionDelegate 用于提供在您的内容上执行的一组操作以及自定义该内容的预览的方法。
    • UIContextMenuConfiguration 包含上下文菜单的配置详细信息的对象
    • UIContextMenuContentPreviewProvider 预览内容时要使用的自定义视图控制器提供者
    • UIContextMenuActionProvider 基于UIAction的上下文菜单提供者
    • UITargetedPreview
    • UIMenu 是用于将应用程序菜单或上下文菜单中的相关菜单元素分组的容器
    • UIAction 要执行的菜单元素

    3. 使用: 通过UIView唤起菜单栏

      1. 给目标View(本文以imgView 作为目标view),添加一个交互对象(UIContextMenuInteraction实例
    // 给imgView添加一个上下文菜单交互
    UIContextMenuInteraction *interaction = [[UIContextMenuInteraction alloc] initWithDelegate:self];
    [_imgView addInteraction:interaction];
    
      1. 实现UIContextMenuInteractionDelegate协议
    /// 交互开始时调用,location: 用户触摸的坐标(在imgView中的位置)
    - (nullable UIContextMenuConfiguration *)contextMenuInteraction:(UIContextMenuInteraction *)interaction configurationForMenuAtLocation:(CGPoint)location
    {
        // 返回一个UIContextMenuConfiguration对象 (对象创建见下文)
        // 用以提供预览控制器和操作菜单,
        return ...
    }
    

    2.1 创建UIContextMenuConfiguration对象 (包含对象如下)

    • 创建UIContextMenuContentPreviewProvider对象
    • 创建UIContextMenuActionProvider对象
      • 创建UIMenu对象
      • 创建UIAction对象

    a. 创建UIAction对象

    // copyAction
    [UIAction actionWithTitle:@"Copy"
                        image:[UIImage systemImageNamed:@"doc.on.doc"]
                   identifier:@"copy"
                      handler:^(__kindof UIAction * _Nonnull action) {... }];
    

    b. 创建UIMenu对象

    // 创建子菜单Actions
    // ...
    
    // 创建子菜单Menu
    // options <= UIMenuOptionsDisplayInline 黑色样式,自动展开子菜单
    // options >= UIMenuOptionsDestructive 红色样式,不展开子菜单
    UIMenu *editMenu = [UIMenu menuWithTitle:@"Edit"
                                       image:[UIImage systemImageNamed:@"pencil.and.outline"]
                                  identifier:@"edit"
                                     options:UIMenuOptionsDisplayInline
                                    children:@[copyAction,duplicateAction]];
    
    // 创建主菜单Actions
    // ...
    
    // 创建主菜单Menu,children是UIMenu和UIAction对象的集合
    UIMenu *menu =[UIMenu menuWithTitle:@"" children:@[editMenu,shareAction,deleteAction]];
    

    c. 创建UIContextMenuActionProvider对象

    UIContextMenuActionProvider actionProvider;
    actionProvider = ^UIMenu * _Nullable(NSArray<UIMenuElement *> * _Nonnull suggestedActions) {
            
         // 创建UIMenu   
         //  ...
       
        return menu; // 返回主菜单对象
    };
    

    d. 创建UIContextMenuContentPreviewProvider对象

    // 预览控制器提供者
     UIContextMenuContentPreviewProvider previewProvider = nil;
     previewProvider = ^UIViewController * _Nullable{
       
       // 如果previewProvider=nil或return nil,将显示默认的将交互目标View作为预览对象   
       return [TLScreenshotController new];
    };
    

    e. 创建UIContextMenuConfiguration对象

    // 创建previewProvider 和 actionProvider
    // ...
    
    [UIContextMenuConfiguration configurationWithIdentifier:@"menus"
                                            previewProvider:previewProvider
                                             actionProvider:actionProvider];
    

    2.2 其他可选代理方法

    /// 返回触发菜单后,要显示的预览目标视图,默认(不实现或返回nil)为触发菜单的视图(imgView)
    - (nullable UITargetedPreview *)contextMenuInteraction:(UIContextMenuInteraction *)interaction previewForHighlightingMenuWithConfiguration:(UIContextMenuConfiguration *)configuration {
        // 返回的预览视图必须是在已经存在当前窗口的视图
        if (self.preViewSgmt.selectedSegmentIndex == 1) {
            return [[UITargetedPreview alloc] initWithView:self.previewImg]; // 显示previewImg
        }
        
        return nil; // 显示imgView
    }
    
    
    /// 返回dismiss时的预览目标视图
    - (nullable UITargetedPreview *)contextMenuInteraction:(UIContextMenuInteraction *)interaction previewForDismissingMenuWithConfiguration:(UIContextMenuConfiguration *)configuration {
        if (self.isShowDismissPreview.on) {
            return [[UITargetedPreview alloc] initWithView: self.previewImg];
        }
        
        return nil;
    }
    
    // MARK: Menu Life Cycle Observer
    /// 当交互将要“提交”,以响应用户点击 "预览" 时调用。
    - (void)contextMenuInteraction:(UIContextMenuInteraction *)interaction willPerformPreviewActionForMenuWithConfiguration:(UIContextMenuConfiguration *)configuration animator:(id<UIContextMenuInteractionCommitAnimating>)animator {
        NSLog(@"willPerformPreviewAction");
    }
    
    /// 将要显示菜单时调用(先调用后显示)
    - (void)contextMenuInteraction:(UIContextMenuInteraction *)interaction willDisplayMenuForConfiguration:(UIContextMenuConfiguration *)configuration animator:(nullable id<UIContextMenuInteractionAnimating>)animator {
        NSLog(@"willDisplayMenu");
    }
    
    /// 交互即将结束时调用(隐藏菜单前调用)
    - (void)contextMenuInteraction:(UIContextMenuInteraction *)interaction willEndForConfiguration:(UIContextMenuConfiguration *)configuration animator:(nullable id<UIContextMenuInteractionAnimating>)animator {
        NSLog(@"willEnd");
    }
    

    4. 使用: 通过UITableViewCell或UICollectionViewCell唤起菜单栏

    • 只需实现UITableView或UICollectionView的本身代理即可,不必绑定一个交互对象
    • TableViewCell
    - (nullable UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point {
        // return 一个UIContextMenuConfiguration对象,对象创建见上文
    }
    
    - (nullable UITargetedPreview *)tableView:(UITableView *)tableView previewForHighlightingContextMenuWithConfiguration:(UIContextMenuConfiguration *)configuration {
        // 返回一个唤起时的预览视图
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:self.indexPath];
        return [[UITargetedPreview alloc] initWithView:cell]; 
    }
    
    - (nullable UITargetedPreview *)tableView:(UITableView *)tableView previewForDismissingContextMenuWithConfiguration:(UIContextMenuConfiguration *)configuration {
        // 返回一个Dismiss时的预览视图
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:self.indexPath];   
        return [[UITargetedPreview alloc] initWithView:cell.imageView];
    }
    //  ... 其他代理方法 ...
    
    • UICollectionViewCell
    - (nullable UIContextMenuConfiguration *)collectionView:(UICollectionView *)collectionView contextMenuConfigurationForItemAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point {
       // return 一个UIContextMenuConfiguration对象,对象创建见上文
    }
    
    
    - (nullable UITargetedPreview *)collectionView:(UICollectionView *)collectionView previewForHighlightingContextMenuWithConfiguration:(UIContextMenuConfiguration *)configuration {
        // 返回一个唤起时的预览视图
    }
    
    - (nullable UITargetedPreview *)collectionView:(UICollectionView *)collectionView previewForDismissingContextMenuWithConfiguration:(UIContextMenuConfiguration *)configuration {
       // 返回一个Dismiss时的预览视图
    }
    //  ... 其他代理方法 ...
    

    相关文章

      网友评论

        本文标题:UIMenus的简单使用

        本文链接:https://www.haomeiwen.com/subject/koiimctx.html