TapGestureRecognizer 在 UIView 子视图中不起作用

Posted

技术标签:

【中文标题】TapGestureRecognizer 在 UIView 子视图中不起作用【英文标题】:TapGestureRecognizer not working in UIView subviews 【发布时间】:2021-06-02 15:12:10 【问题描述】:

我想向自定义 UIView(代表图像和标签)添加轻击手势识别器。似乎手势识别器没有添加到视图中,或者子视图不被视为 UIView 本身,因此不起作用。

这是我添加视图的方法:

Navbar.swift :
let indexSubview = IconTextView(svgName: "https://placeholder.pics/svg/30", textKey: "Index")
self.indexButton.addSubview(indexSubview)
let indexButtonTap = UITapGestureRecognizer(target: self, action: #selector(goToIndex))
indexButton.addGestureRecognizer(indexButtonTap)

(IconTextView 是我的自定义视图)

然后当我点击indexButtonnothing 时。

我的点击功能,以防万一:

@objc func goToIndex(sender:UITapGestureRecognizer) 
        print("GO TO INDEX")
        router.setRoute(routeName: "INDEX", routeParam: "")
    

我不明白为什么它不起作用,所有元素都启用了 userInteractions。

【问题讨论】:

【参考方案1】:

你的先例代码+点击手势,我编辑约束,添加containerView:

class Aiutotipo: UIViewController 

let myImageView: UIImageView = 
    
    let iv = UIImageView()
    iv.contentMode = .scaleToFill
    iv.clipsToBounds = true
    iv.backgroundColor = .red
    iv.translatesAutoresizingMaskIntoConstraints = false
    
    return iv
()

let myLabel: UILabel = 
    let label = UILabel()
    label.text = "Débats"
    label.textColor = .white
    label.textAlignment = .center
    label.font = .systemFont(ofSize: 30, weight: .semibold)
    label.translatesAutoresizingMaskIntoConstraints = false
    
    return label
()

let containerView = UIView() //your container view

override func viewDidLoad() 
    super.viewDidLoad()
    
    containerView.backgroundColor = .red // red bacground for container view visible, set .clear for transparent bg
    containerView.isUserInteractionEnabled = true
    containerView.translatesAutoresizingMaskIntoConstraints = false
    
    let indexButtonTap = UITapGestureRecognizer(target: self, action: #selector(goToIndex))
    containerView.addGestureRecognizer(indexButtonTap)
    
    myImageView.image = UIImage(named: "profilo") // set here your image
    
    let myWidth = myLabel.intrinsicContentSize.width // This reveal only text width in label
    
    view.addSubview(containerView)
    containerView.heightAnchor.constraint(equalToConstant: myWidth + 50).isActive = true
    containerView.widthAnchor.constraint(equalToConstant: myWidth).isActive = true
    containerView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    containerView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    
    containerView.addSubview(myImageView)
    myImageView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
    myImageView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
    myImageView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
    myImageView.heightAnchor.constraint(equalTo: myImageView.widthAnchor).isActive = true
    
    containerView.addSubview(myLabel)
    myLabel.topAnchor.constraint(equalTo: myImageView.bottomAnchor).isActive = true
    myLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
    myLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
    myLabel.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true


@objc fileprivate func goToIndex() 
    print("GO TO INDEX")
 

这是结果

【讨论】:

以上是关于TapGestureRecognizer 在 UIView 子视图中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

嵌套网格 TapGestureRecognizer 不工作?

如何为多个 UILabel 创建一个 tapGestureRecognizer?

iOS - TapGestureRecognizer - 点击适用于整个屏幕而不是视图

UIToolbarButton 上的 TapGestureRecognizer 不起作用

TapGestureRecognizer 在 UIView 子视图中不起作用

TapgestureRecognizer 将触摸检测为滚动视图