0

I have image slider .. i followed this tutorial to do it: Create a Horizontal Paging UIScrollView with UIPageControl Swift 4 XCode 9 and it's working perfectly..

now i want to add gesture recognizer on these images .. i tried to do it like this:

func createSlides() -> [banner] {

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapped))
    tapGesture.numberOfTapsRequired = 1
    tapGesture.numberOfTouchesRequired = 1

    let slide1:banner = Bundle.main.loadNibNamed("banner", owner: self, options: nil)?.first as! banner
    slide1.img.image = UIImage(named: "bannerex")
    slide1.img.addGestureRecognizer(tapGesture)

    let slide2:banner = Bundle.main.loadNibNamed("banner", owner: self, options: nil)?.first as! banner
    slide2.img.image = UIImage(named: "bannerex")
    slide2.img.addGestureRecognizer(tapGesture)

    let slide3:banner = Bundle.main.loadNibNamed("banner", owner: self, options: nil)?.first as! banner
    slide3.img.image = UIImage(named: "bannerex")
    slide3.img.addGestureRecognizer(tapGesture)

    return [slide1, slide2, slide3]
}

@objc func tapped(_ sender: UITapGestureRecognizer){
    print("tapped")
    print(pageControl.currentPage)
}

but it's not working, nothing will happen.

How to solve this issue?

import UIKit

class banner: UIView {


@IBOutlet weak var img: UIImageView!

 }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
mrs.tat
  • 597
  • 2
  • 6
  • 17

3 Answers3

3

There are two problems here:

  1. isUserInteractionEnabled is false by default
  2. you have to assign each image view a separate gesture recognizer

Solution

Instead of setting isUserInteractionEnabled to true for each image view individually, I suggest placing that code in the Banner class itself. Since it seems your initializing it from the storyboard, using the awakeFromNib function should work just fine:

class Banner: UIView {

    @IBOutlet weak var img: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()
        img.isUserInteractionEnabled = true
    }
 }

In your createSlides() function, add a separate tap gesture to each view:

let slide1:banner = Bundle.main.loadNibNamed("banner", owner: self, options: nil)?.first as! banner
slide1.img.image = UIImage(named: "bannerex")
slide1.img.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapped)))

or even better for scaling & no-code-redundancies:

function tap() -> UITapGestureRecognizer {
    return UITapGestureRecognizer(target: self, action: #selector(tapped))
}

let slide1:banner = Bundle.main.loadNibNamed("banner", owner: self, options: nil)?.first as! banner
slide1.img.image = UIImage(named: "bannerex")
slide1.img.addGestureRecognizer(tap())
LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
2

The UITapGestureRecognizer must be used with a single view only so you need to create different UITapGestureRecognizer for each image if you want to detect tap on each image. Read more HERE.

And you need to set isUserInteractionEnabled to true for every imageView.

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
0

Image views ignore user events by default. If you want an image view to handle user interactions as well, change the value of its isUserInteractionEnabled property to true.

Rocky
  • 2,903
  • 1
  • 22
  • 26
  • I just set that to true for all of the images in the array .. and worked only for the last image view in the list .. why? – mrs.tat Feb 21 '19 at 08:05