iOS開發之Context Menus

yungfan· 2020-07-20

介紹

  • 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])
    }
}

我的微信公眾號

定期發布 Swift、SwiftUI、Combine、iOS開發等技術文章,也會更新一些自己的學習心得,歡迎大家關注。

丰禾棋牌官网1369 上海电气股票行情 河北排列l七开奖结果 云智在线 杨方股票配资平台 河北十一选五前三组最大遗漏 中国股指期货配资网 管家婆神童平特一肖图 安微11选5直选走试图 微信理财平台 广东好彩一怎么买稳赚 天津快乐十分快五最大遗漏