Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add tapGesture for UIImageView in UIView

Tags:

ios

swift

gesture

I am stuck in adding tap gesture recogniser for UIImageView in a UIView. I did put the isUserInteractionEnabled = true for both UIImageView And UIView. but it still not working.

@IBOutlet weak var adView: UIView!<br>
@IBOutlet weak var defaultBannerView: UIImageView!
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(FavouritesVC.tappedImageBanner(_:)))

defaultBannerView.addGestureRecognizer(tapGesture) defaultBannerView.isUserInteractionEnabled = true adView.isUserInteractionEnabled = true

 func tappedImageBanner(_ sender: UIGestureRecognizer){
    print("Tapped")
    let tappedImageView = sender.view!
    let imageView = tappedImageView as! UIImageView
    print(imageView) }

Edit:

I added adView.addGestureRecognizer(tapGesture) but the error is 'Could not cast value of type 'UIView' to 'UIImageView''

Actually I am doing admob, I want to put my default Banner when I can't request the banner from google admob. So I want user to able to click on my default banner and direct to the website. So should I just add the tapGesture in the adView and hide the imageView when I can get the ad from google?

what if I want to add gesture in UIImageView? how should I do that?

like image 844
Andrew Avatar asked Sep 11 '25 20:09

Andrew


1 Answers

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

func imageTapped(tapGestureRecognizer: UITapGestureRecognizer){

    let tappedImage = tapGestureRecognizer.view as! UIImageView

    // Your action 
}
like image 196
Dhananjay Patil Avatar answered Sep 13 '25 09:09

Dhananjay Patil