点击手势事件不适用于 UIView

Tap gesture event not working on UIView

我正在尝试向 UIView 添加点击手势,但该手势未被识别。

"iconBadgeView" 是一个 UIView,其中包含在参数中传递的已定义大小的图像。

lazy var cardioVascularIcon : IconBadgeView! = {

    let iconBadgeView = IconBadgeView(frame: CGRect(x: 0, y: 0, width: 95, height: 95), data:["big":"db_history"])

    let tapEvent = UITapGestureRecognizer(target: self, action: #selector(loadNewView(sender:)))

    tapEvent.numberOfTapsRequired = 1
    iconBadgeView.translatesAutoresizingMaskIntoConstraints = false

    iconBadgeView.isUserInteractionEnabled = true       
    iconBadgeView.addGestureRecognizer(tapEvent)
}()

同一个 class 有一个委托人,功能实现如下:

func loadNewView(sender: UITapGestureRecognizer)  {
    print("Tapped")
}

函数 loadNewView 没有被调用。我不确定代码中有什么问题。如果有人可以帮助,请。

我正在将 iconBadgeView 添加到超级视图中,如下所示:

override init(frame: CGRect) {
    super.init(frame: CGRect.zero)

    addSubview(cardioVascularIcon)

    cardioVascularIcon.pinToSuperview([.Top])
    cardioVascularIcon.pinToSuperview([.Left], constant: 92)
    cardioVascularIcon.pinToSuperview([.Right], constant: 92)
}

您的 iconBadgeView 消失了,因为它是局部变量。

您必须初始化 cardioVascularIcon 变量。

lazy var cardioVascularIcon : IconBadgeView! = {

  cardioVascularIcon.frame = CGRect(x: 0, y: 0, width: 95, height: 95)
  //here call function which sets data property

  let tapEvent = UITapGestureRecognizer(target: self, action: #selector(loadNewView(sender:)))

  tapEvent.numberOfTapsRequired = 1
  cardioVascularIcon.translatesAutoresizingMaskIntoConstraints = false

  cardioVascularIcon.isUserInteractionEnabled = true       
  cardioVascularIcon.addGestureRecognizer(tapEvent)
}()

我找到了解决该问题的方法。我使用了按钮而不是标签,现在一切正常。下面是我正在使用的代码:

func createButton (buttonWidth : CGFloat?, buttonTitle : String?, buttonFont : UIFont?, imageName : String?, buttonColor : UIColor?) -> UIButton {
    let newButton = UIButton(type: . custom)
    newButton.showsTouchWhenHighlighted = false
    newButton.translatesAutoresizingMaskIntoConstraints = false
    newButton.adjustsImageWhenHighlighted = false

    if let title = buttonTitle {
        newButton.setTitle(title, for: .normal)
    }
    if let color = buttonColor {
        if let _ = newButton.titleLabel {
            newButton.setTitleColor(color, for: .normal)
        }
    }

    if let btnWidth = buttonWidth {
        newButton.frame = CGRect(x: 0, y: 0, width: btnWidth, height: btnWidth)
        newButton.layer.cornerRadius = 0.5 * newButton.bounds.size.width
        newButton.clipsToBounds = true
    }
    if let img = imageName {
        newButton.setImage(UIImage(named: img), for: .normal)
    }
    if let font = buttonFont {
        newButton.titleLabel?.font = font
    }

    return newButton
}
let addDiagButton = self.createButton(buttonWidth: nil, buttonTitle: addButtonTitle, buttonFont: UIFont.regularDisplayOfSize(30), imageName: nil, buttonColor: UIColor(red: 111, green: 160, blue: 186))

addDiagButton.addTarget(self, action: #selector(addDiag(sender:)), for: .touchUpInside)

上面的代码有一个共同的功能,它创建一个按钮并附加触发事件。代码运行良好。

为了使其表现得像标签点击,我在 createButton 函数中添加了一行。

newButton.adjustsImageWhenHighlighted = 假

这将限制点击按钮时的闪光效果。