Swift: UITapGestureRecognizer - Not able to have gesture listening for imageView

57 Views Asked by At

TapGestureRecognizer this is the code for the initialization of the image view with tap gesture recognizer.

 var drawerImageView :UIImageView = {
            var drawerImageView = UIImageView()
            drawerImageView.image = UIImage(systemName: "list.dash")
            let tap = UITapGestureRecognizer(target: HomeViewController.self, action: #selector(tapFunction(sender:)))
            tap.numberOfTapsRequired = 1
            tap.numberOfTouchesRequired = 1
            drawerImageView.addGestureRecognizer(tap)
            drawerImageView.isUserInteractionEnabled = true
            return drawerImageView
        }()

And here below i am setting the constraints for the image view.

  drawerImageView.translatesAutoresizingMaskIntoConstraints = false
        drawerImageView.leadingAnchor.constraint(equalTo: view.leadingAnchor,constant: 30).isActive = true
        drawerImageView.topAnchor.constraint(equalTo: view.topAnchor,constant: 40).isActive = true
        drawerImageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
        drawerImageView.heightAnchor.constraint(equalToConstant: 100).isActive = true

The problem is i am not able to listen to taps only when i set the width height to more than 200 then only i am receiving the tap events.

this is the objc method for the above tap gesture which will need to call this method but i am not able to receive the control here. @objc func tapFunction(sender:UITapGestureRecognizer) { print("image tap") } This issue is not clear to understand.

2

There are 2 best solutions below

3
willow On

Your question is a little hard to notice for me. But I believe you have problem by receiving touch event, if so, the problem is this.

write your touch event outside of your computed variable.

import UIKit

class ViewController: UIViewController {
    
    
    var drawerImageView :UIImageView = {
        var drawerImageView = UIImageView()
        drawerImageView.image = UIImage(systemName: "list.dash")
        drawerImageView.isUserInteractionEnabled = true
        return drawerImageView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(drawerImageView)
        drawerImageView.translatesAutoresizingMaskIntoConstraints = false
        drawerImageView.leadingAnchor.constraint(equalTo: view.leadingAnchor,constant: 30).isActive = true
        drawerImageView.topAnchor.constraint(equalTo: view.topAnchor,constant: 40).isActive = true
        drawerImageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
        drawerImageView.heightAnchor.constraint(equalToConstant: 100).isActive = true
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction(sender:)))
        tap.numberOfTapsRequired = 1
        tap.numberOfTouchesRequired = 1
        drawerImageView.addGestureRecognizer(tap)
        
    }
    
    @objc func tapFunction(sender:UITapGestureRecognizer) { print("image tap") }
    
    
}

if you want to receive touch when your size is more than 200, just need to write an if in your "tapFunction" and check if the size of your image in more than 200.

0
willow On

it is not about the size of your image, the way you are implementing navigation-controller is not good. it seems you are locating your images under navigationController topbar which is hidden( I just highlighted the part which is hidden). change the position of your image and you will see your tap is ran or if you tap on the part of you image which is not behind the topper, your touch again works. so the part of your image is located behind naviationbar so the touch won't respond to it. I saw you code for navigation, and it wasn't proper.

Also if you let your rootViewcontroller directly load HomeViewcontroller, you will see your code works just fine because your navigationController won't interrupt your touch event.

window.rootViewController =  HomeViewController()

enter image description here