用户图像的 ContextMenuConfiguration?

ContextMenuConfiguration for User Images?

我希望用户的图像弹出(没有操作选项),然后在触摸外部时被关闭。

假设 table 视图由 2 UI 个元素(一个按钮和一个文本)和一张图片组成。你如何设置只为图片弹出上下文菜单 - immy?

public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as!  ViewControllerTableViewCell
    let immy = cell.viewWithTag(1) as! UIImageView
    let person: Userx = people[indexPath.row]
    let button = cell.viewWithTag(3) as! UIButton
    cell.lblName.text = person.Education
    cell.postID = self.people[indexPath.row].postID
    if let PhotoPosts = person.PhotoPosts {
        let url = URL(string: PhotoPosts)
        print(PhotoPosts, "sttdb")
        immy.sd_setImage(with: url)
    }
    return cell
}

extension homepage {
    func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
        let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil)
        return configuration
    }
}

下面是ViewControlleTableViewCell,添加在第一条评论之后。我对评论的回复也解释了这在 main.storyboard.

中的样子
class ViewControllerTableViewCell: UITableViewCell {    

    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var immy: UIImageView!
    @IBOutlet weak var lblgenre1: UILabel!    
    @IBOutlet weak var lblName: UILabel!
    @IBOutlet weak var lblName1: UILabel!
    @IBOutlet weak var likeLabel: UILabel!

    var postID: String!
    var caption1: String!
    var keyToPost: String!
    var latestLocation1: [String: Double]?
    let databaseRef = Database.database().reference()
    var refArtists: DatabaseReference!

    let profileImageView: UIImageView = {
        let imageView = UIImageView()
        
        return imageView
    } ()
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    @IBAction func button1(_ sender: Any) {

    }
    @IBAction func button2NBTHISISTORPREVENTSEGUE(_ sender: Any) {

    }
}

首页:

 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell

您需要在您的 UIImageView 中启用用户交互,使您的 ViewControllerTableViewCell 符合 UIContextMenuInteractionDelegate,创建一个 UIContextMenuInteraction 并将您的单元格设置为委托.然后将迭代添加到您的图像视图。不要忘记从主页扩展中删除 contextMenuInteraction 方法。

您的 ViewControllerTableViewCell contextMenuInteraction 交互实现应如下所示:

import UIKit

class ViewControllerTableViewCell: UITableViewCell, UIContextMenuInteractionDelegate {
    @IBOutlet weak var immy: UIImageView!
    override func awakeFromNib() {
        super.awakeFromNib()
        immy.isUserInteractionEnabled = true
        immy.addInteraction(UIContextMenuInteraction(delegate: self))
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
        UIContextMenuConfiguration(identifier: nil, previewProvider: nil)  { _ in
            let share = UIAction(title: "Share", image: UIImage(systemName: "square.and.arrow.up")) { _ in
               // share code
            }
            return UIMenu(title: "Profile Picture Menu", children: [share])
        }
    }
}