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?
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With