如何为两个UIView中的UIIMageView添加触摸处理程序

问题描述 投票:0回答:1

我通过创建一个名为TitleView的UIView创建了一个自定义导航titleview,然后在其中存储了一个名为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!!!")
    }

我不确定为什么这行不通。为了尝试解决此问题,我在导航栏中启用了TitleInterview和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个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.