【问题标题】:How to add a touch handler for a UIIMageView that is within two UIViews如何为两个 UIView 中的 UIIMageView 添加触摸处理程序
【发布时间】:2020-10-07 19:43:50
【问题描述】:

我通过创建一个名为 TitleView 的 UIView 创建了一个自定义导航标题视图,然后我在其中存储了一个名为 containerView 的 UIView,其中包含一个名为 profileImageView 的 UIImageView 和一个 UILabel。我想在 UIImageView 上启用触摸处理程序。所以我尝试像这样添加它:

        //add touch handler to Profile Image to segue to expanded view
        let singleTap = UITapGestureRecognizer(target: self, action: #selector(expandProfile))
        profileImageView.isUserInteractionEnabled = true
        singleTap.numberOfTouchesRequired = 1
        profileImageView.addGestureRecognizer(singleTap)

到目前为止,触摸处理程序只是打印 - 但它从未被调用

    //action for user clicking on the profile image view
    @objc func expandProfile() {
        print("THIS WORKS!!!")
    }

我不确定为什么这不起作用。为了尝试解决这个问题,我在导航栏中的 TitleView 和 Container View 中启用了 UserInteraction,但这并没有解决?我不确定是什么导致了问题

编辑以查看设置我的导航栏的代码:

    //Setting Up nav bar to have image and name
    func setupNavBar(){
        //We first create a titleview which we will use as the custom titleview in the navigation bar
        let titleView  = UIView()
        titleView.isUserInteractionEnabled = true
        titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 60)
        //ContainerView will be within the titleview so that it allows it to grow dynamically with size of the name
        let containerView = UIView()
        containerView.isUserInteractionEnabled = true
        containerView.translatesAutoresizingMaskIntoConstraints = false
        titleView.addSubview(containerView)
        //The view for storing the Profile picture of the match
        let profileImageView = UIImageView()
        profileImageView.loadImageUsingCacheWithUrlString(urlString: matchImageURLs[0])
        profileImageView.translatesAutoresizingMaskIntoConstraints = false
        profileImageView.contentMode = .scaleAspectFill
        profileImageView.layer.cornerRadius = 20
        profileImageView.clipsToBounds = true
        containerView.addSubview(profileImageView)
        //Constraints for Profile Image
        profileImageView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
        profileImageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
        profileImageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
        profileImageView.heightAnchor.constraint(equalToConstant: 40).isActive = true
        //add touch handler to Profile Image to segue to expanded view
        self.navigationController?.navigationBar.isUserInteractionEnabled = true
        let singleTap = UITapGestureRecognizer(target: self, action: #selector(expandProfile))
        profileImageView.isUserInteractionEnabled = true
        singleTap.numberOfTouchesRequired = 1
        profileImageView.addGestureRecognizer(singleTap)
        self.view.bringSubviewToFront(profileImageView)
        //View for the name of the match
        let nameLabel = UILabel()
        containerView.addSubview(nameLabel)
        nameLabel.text = matchName
        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        //Name Label constraints
        nameLabel.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, constant: 8).isActive = true
        nameLabel.centerYAnchor.constraint(equalTo: profileImageView.centerYAnchor).isActive = true
        nameLabel.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
        nameLabel.heightAnchor.constraint(equalTo: profileImageView.heightAnchor).isActive = true

        containerView.centerXAnchor.constraint(equalTo: titleView.centerXAnchor).isActive = true
        containerView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true


        self.navigationItem.titleView = titleView
        //Adding the side menu button
        let elipsesImage = UIImage(systemName: "ellipsis")

        let menuItem = UIBarButtonItem(image: elipsesImage, style: .plain, target: self, action: #selector(menuTapped))
        menuItem.tintColor = UIColor.init(red: 238/255, green: 119/255, blue: 98/255, alpha: 1)
        navigationItem.rightBarButtonItem = menuItem
        rightBarDropDown.anchorView = menuItem
        rightBarDropDown.dataSource = ["Un-Match","Block", "View Profile"]
        rightBarDropDown.cellConfiguration = { (index, item) in return "\(item)" }

    }

【问题讨论】:

  • 我试试你的代码,它工作正常。也许你可以分享你所有的代码?
  • 我为设置导航栏的功能添加了代码

标签: ios swift uiview uigesturerecognizer


【解决方案1】:

您添加个人资料图片的一个或多个视图可能会将用户交互设置为 false。我建议您将点击手势设置在 containerView 而不是个人资料图像本身上。

【讨论】:

  • 即使我将它添加到容器视图中它也不起作用,只有当我将它添加到导航栏本身时它才起作用。您可以在我的函数中看到,我在所有不同的视图上都将用户交互设置为 true,这让我对它为什么不起作用感到困惑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-18
  • 2010-12-02
  • 2011-06-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多