4

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?

RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
Andrew
  • 75
  • 1
  • 6
  • https://stackoverflow.com/a/27880669/8432814 refer this answer – Hakikat Singh Sep 26 '17 at 08:30
  • 1
    Possible duplicate of [How to assign an action for UIImageView object in Swift](https://stackoverflow.com/questions/27880607/how-to-assign-an-action-for-uiimageview-object-in-swift) – RajeshKumar R Sep 26 '17 at 08:33
  • add a UIButton over UIImageView andd handle the action over it. – cpt. Sparrow Sep 26 '17 at 08:39
  • you have to create two tap gestures for uiview and uiimageview and then use yourview.addGestureRecognizer(tapGesture) yourImageview.addGestureRecognizer(tapGesture) – Anil Kumar Sep 26 '17 at 08:41
  • Thanks for fast respond, I did refer to the link but it doesnt work in my case. I Added a custom UIView called adView, and inside to the customView I added the UIImageView. I tried to put the isUserInteractionEnabled for both adView and defaultBannerView. it is working. But what I wanted is just the tapGesture in defaultBannerView. if I didn't addGestureRecognizer for the adView, it won't allow me to touch the defaultBannerView.... So should I just add the tap gesture in the view instead? – Andrew Sep 26 '17 at 08:42
  • check this link https://stackoverflow.com/questions/28522104/how-to-make-a-uiimageview-tappable-and-cause-it-to-do-something-swift – Suja Jan 08 '20 at 05:32

2 Answers2

10
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 
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Dhananjay Patil
  • 442
  • 3
  • 10
  • I tried, it doest work in my case... Because I put the imageView inside of a custom UIview. now it works when I added the isUserInteractionEnabled and addGestureRecognizer for the UIView. But what I wanted is just the gesture in UIimageview... – Andrew Sep 26 '17 at 08:45
  • Thanks a lot, you saved my day, userInteractionEnabled did the magic for me, Thanks – Sakthimuthiah Sep 07 '20 at 13:09
0

I am stuck in adding tap gesture recogniser for UIImageView in a UIView. (...) but it still not working.

What does that mean: not working? I can assume that taps are not detected.

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

When the error happens? I assume that it comes when adView is tapped, so that because your sender.view! is not a UIImageView (in case gesture recogniser is linked to UIView).

I want to put my default Banner when I can't request the banner from google admob. (...) 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?

Have you ever considered adding UIImageView above UIView and change its alpha to 0 or hide when Banner is loaded - it prevents imageView from being tapped?

Actually I am doing admob (...) I Added a custom UIView called adView (...) it won't allow me to touch the defaultBannerView

As you say, you are adding imageView to custom view and you don't know how it really works. Think if you really need to interfere with its logic.

Forgetter
  • 16
  • 1
  • 2
  • Thanks for the reply, Yes. 1- You are right, the taps are not detected in imageView. 2- Yes I think so, so it caused an error. 3- You mean place the imageView in Custom View? Yes, currently I'm doing that but then the gesture will only works when I place the gesture function to the custom view. Then when the banner loaded, I set the imageView to hidden and remove gestures. Is that right? 4- As you mentioned for the last sentence, yes, I'm new to iOS developer, may I know what is the reason that imageView in custom View doesn't work like that? – Andrew Sep 27 '17 at 01:38