iOS开发之Context Menus

1,471 阅读1分钟

介绍

  • WWDC 2019 推出了上下文菜单(Context Menus)成为 3DTouch 的替代品。
  • 需要通过长按方式触发。
  • 如果要启用上下文菜单,需要创建一个 UIContextMenuInteraction 并将其添加给某个触发的 UIView,然后指定 delegate,实现代理方法。
  • 代理方法需要返回一个UIContextMenuConfiguration,其构造函数如下init(identifier: NSCopying?, previewProvider: UIContextMenuContentPreviewProvider?, actionProvider: UIContextMenuActionProvider? = nil),最主要是第三个参数,需要在其中创建UIMenu
  • 案例
class ViewController: UIViewController {
    
    // 需要打开User Interaction
    @IBOutlet weak var imageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 创建UIContextMenuInteraction
        let interaction = UIContextMenuInteraction(delegate: self)
        // 添加UIContextMenuInteraction
        imageView.addInteraction(interaction)
    }
}

// 代理方法
extension ViewController: UIContextMenuInteractionDelegate {
    func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
        
        // 第一个菜单
        let favorite = UIAction(title: "Favorite", image: UIImage(systemName: "heart.fill")) { action in
            print("favorite")
        }
        
        // 第二个菜单
        let share = UIAction(title: "Share", image: UIImage(systemName: "square.and.arrow.up.fill")) { action in
            print("share")
        }
        
        // 第三个菜单
        let delete = UIAction(title: "Delete", image: UIImage(systemName: "trash.fill"), attributes: [.destructive]) { action in
            print("delete")
        }
        
        // 返回UIContextMenuConfiguration
        return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { _ in
            UIMenu(title: "Actions", children: [favorite, share, delete])
        }
    }
}
  • 效果

UITableView和UICollectionView

iOS 13 以后,UITableView 和 UICollectionView 也支持 Context Menus,使用起来特别简单,只需要实现相应的代理方法,返回UIContextMenuConfiguration即可。

  • UITableView
func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
    
    let favorite = UIAction(title: "Favorite", image: UIImage(systemName: "heart.fill")) { action in
        print("favorite")
    }
    
    let share = UIAction(title: "Share", image: UIImage(systemName: "square.and.arrow.up.fill")) { action in
        print("share")
    }
    
    let delete = UIAction(title: "Delete", image: UIImage(systemName: "trash.fill"), attributes: [.destructive]) { action in
        print("delete")
    }
    
    return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { _ in
        UIMenu(title: "Actions", children: [favorite, share, delete])
    }
}
  • UICollectionView
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? { 
    
    let favorite = UIAction(title: "Favorite", image: UIImage(systemName: "heart.fill")) { action in
        print("favorite")
    }
    
    let share = UIAction(title: "Share", image: UIImage(systemName: "square.and.arrow.up.fill")) { action in
        print("share")
    }
    
    let delete = UIAction(title: "Delete", image: UIImage(systemName: "trash.fill"), attributes: [.destructive]) { action in
        print("delete")
    }
    
    return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { _ in
        UIMenu(title: "Actions", children: [favorite, share, delete])
    }
}

源代码

Context Menus