Taxi Noi Bai Ha Noi Taxi Noi Bai Ha Noi - 4 months ago 32
iOS Question

Recognize which View calls the Tap function

I know how to add tappability to the

, however, there are 2 image views and I want to distinguish them to call the correct function. However, I can't seem to get the correct

func addTappability (view imageView:UIImageView){
//add tapping function for image

let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:#selector(IdCardViewController.imageTapped(_:)))
imageView.isUserInteractionEnabled = true

func imageTapped(_ sender: UIImageView) {

//Problem here, can't get correct sender
if ( sender == photoImageViewLeft) {
//do one thing
}else {
//do the other


Replace your function with this:

func imageTapped(_ sender: UITapGestureRecognizer) {

    if let imageView = sender.view as? UIImageView {
        if ( imageView == photoImageViewLeft ) {
            print("Image1 Tapped")
        }else {
            print("Image2 Tapped")