Search code examples
swiftuiviewuitapgesturerecognizer

Tap gesture event not working on UIView


I am trying to add a tap gesture to a UIView but the gesture is not getting recognized.

"iconBadgeView" is a UIView with an image of defined size as passed in the parameters.

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)
}()

There is a delegator attached to the same class and function is implemented as below :

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

The function loadNewView is not getting called. I am not sure what is wrong here in the code. Please if someone can help.

I am adding iconBadgeView to the superview as below :

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

    addSubview(cardioVascularIcon)

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

Solution

  • I got a workaround for the issue. I have used a button instead of a Label and things are working exactly fine now. Below is the code I am using :

    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)
    

    The above code has a common function which creates a button and also attaches the trigger event with it. The code is working perctely fine.

    To make it behave like a label click, I have added a line in the createButton function.

    newButton.adjustsImageWhenHighlighted = false

    This will restrict the flash effect of the button when clicked.