iOS进阶+实战

iOS开发之Context Menus

2020-07-20  本文已影响0人  YungFan

介绍

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即可。

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])
    }
}
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开发等技术文章,也会更新一些自己的学习心得,欢迎大家关注。


微信公众号
上一篇下一篇

猜你喜欢

热点阅读