简体   繁体   中英

Gesture Recognizer not triggered iOS

I have an image view on scroll view. I added more than one sub views on image view.

->UIScrollView

-->UIImageView

--->UIViews

I want to add gesture recognizer to subviews. But It doesn't work. Where is my mistake? function handleTap() not triggered.

 func addSubViewOnImageView(mPoint:CGPoint, mSize: CGSize){
    let rect = CGRect(origin: mPoint, size: mSize)
    let sView = UIView(frame: rect)
    sView.isUserInteractionEnabled = true
    let tap = UITapGestureRecognizer(target: self,  action: #selector(self.handleTap(_:)))
    tap.delegate = self
    sView.addGestureRecognizer(tap)
    imageView.addSubview(sView)
  }

 @objc func handleTap(_ sender: UITapGestureRecognizer) {
        print("tapped any sub view")
  }

This property is inherited from the UIView parent class. This class changes the default value of this property to NO.

enable the user interaction for your imageview, by default its false , for more info you get the info from apple document

imageView.isUserInteractionEnabled = true

just check

yourImageView.isUserInteractionEnabled = true 

is there or not?

An Alternate if you don't want to do yourImageView.isUserInteractionEnabled = true

Just make your UIView class a subclass of UIControl from storyboard refer from screenshot.

it will act as an UIButton no need to write the code for adding an tap gesture just add an action and like normally we do for UIImageView`

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM