How to Detect Which Image Has Been Tapped in Swift

Detecting which ImageView has has been tapped

As has already been mentioned in comments, you should use a UICollectionView for this kind of work. @Fogmeister has promised to add an answer concerning that later, so I won't do that. But I can answer the actual question, even though it's not what you should do.

From your code I can see that you probably have outlets for all your imageViews (imgView1 ... imgView36) and set each image manually. To detect taps on any of these, you could do something like this:

func viewDidLoad(){
super.viewDidLoad()
let allImageViews = [imageView1, imageView2, .... imageView36]
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didTapImageView(gesture:)))
allImageViews.forEach({$0.addGestureRecognizer(tapGestureRecognizer)})
}

@objc func didTapImageView(gesture:UITapGestureRecognizer){
guard let imageView = gesture.view as? UIImageView else { return }

//Here you can put code that will happen regardless of which imageView was tapped.
imageView.alpha = 0.0

//If you need to know exactly which imageView was tapped, you can just check
if imageView == self.imageView1{
//Do stuff only for imageView1
}else if imageView == self.imageView2{
//...
}//....
}

Again, this is not very good practice. If you go for UICollectionView instead, you don't have to have outlets for all your imageViews and you don't have to create a gestureRecognizer for handling events. But still, I hope this helped you understand general gestures better.

Detect tap on image view

Set the image view's userInteractionEnabled to true and add a UITapGestureRecognizer to the image view.

You can do it in a storyboard by checking the “User Interaction Enabled” checkbox, dragging a tap gesture recognizer onto the image view, and connecting the tap gesture recognizer to an action in your view controller.

storyboard demo

Swift Detect UISlider image tap

Here's how you can do it. Please read the comments carefully.

import UIKit

class ViewController: UIViewController {

// This is linked to your storyboard layout
@IBOutlet weak var slider: UISlider!

override func viewDidLoad() {
super.viewDidLoad()

// Add a tap gesture recognizer to the slider
slider.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(sliderTapped(_:))))
}

@objc private func sliderTapped(_ tapRecognizer: UITapGestureRecognizer) {
let location = tapRecognizer.location(in: slider)

// UISlider can tell you min/max value image frames
// CAUTION: Use images those are big enough to be easy tap targets
// In my case, I used images of size 30x30
let minImageRect = slider.minimumValueImageRect(forBounds: slider.bounds)
let maxImageRect = slider.maximumValueImageRect(forBounds: slider.bounds)

// Now you can check which image out of min/max was tapped
if minImageRect.contains(location) {
print("Min Image Tapped")
} else if maxImageRect.contains(location) {
print("Max Image Tapped")
}
}

}

How recognize which UIImageview I've tapped?

If they are just white and black, it would be much simpler to use a UIView and set its backgroundColor to .white or .black. You can use the tag property of the UIViews to identify them.

In your gestureRecognizer handler, the recognizer.view tells you the view that triggered the gesture.

You can assign the tags in Interface Builder. For example, if you have an 8x8 array of squares, you could use a 2-digit number where the first digit is the row and the second digit is the column. Thus, your tags would be 11, 12, ..., 87, 88.

func squareTapped(recognizer: UIGestureRecognizer) {
if let view = recognizer.view {
let tag = view.tag
let row = tag / 10
let col = tag % 10
print("The view at row \(row), column \(col) was tapped")

if view.backgroundColor == .black {
view.backgroundColor = .white
} else {
view.backgroundColor = .black
}
}
}

If you do want to use images, then load the images as properties of your viewController and assign them based upon the row and column of your image. Here I have used an Outlet Collection to hold all of the UIImageViews. In Interface Builder, you'd connect each of your cells to the squares property.

class BoardViewController: UIViewController {
let blackImage = UIImage(named: "blackImage")!
let whiteImage = UIImage(named: "whiteImage")!
@IBOutlet var squares: [UIImageView]!
var recognizersAdded = false

func setUpBoard() {
for imageview in squares {
if !recognizersAdded {
let recognizer = UITapGestureRecognizer(target: self, action: #selector(squareTapped))
imageview.addGestureRecognizer(recognizer)
imageview.isUserInteractionEnabled = true
}

let tag = view.tag
let row = tag / 10
let col = tag % 10

// Just for demo purposes, set up a checkerboard pattern
if (row + col) % 2 == 0 {
imageview.image = blackImage
} else {
imageview.image = whiteImage
}
}
recognizersAdded = true
}

func squareTapped(recognizer: UIGestureRecognizer) {
if let view = recognizer.view as? UIImageView {
let tag = view.tag
let row = tag / 10
let col = tag % 10
print("The view at row \(row), column \(col) was tapped")

if view.image == blackImage {
view.image = whiteImage
} else {
view.image = blackImage
}
}
}
}

recognize which UIImageView has been tapped and get its index in an array

Since you already have an array storing all your images, it's fairly easy to know what image (and index) has been tapped by searching the image index within houseImages.

Because you don't need complex verifications to get your index with firstIndex(where: [...]), I would suggest to simply use firstIndex(of: UIImageView) in your case.

@IBAction func imageTapped(recognizer: UIGestureRecognizer) {
guard let tappedView = recognizer.view as? UIImageView else {
return
}
let index = self.houseImages.firstIndex(of: tappedView)
print("House image index = \(index)")
}

IOS - Segue - detect which image was clicked

There are a couple of ways to do this. First of all, UIButtons can actually have a background image, which you could set to your various images. If you're intent on using a UIImageView, you would have to implement its touch event receiving method and handle it yourself. Either way, you can use performSegueWithIdentifier:.

Detect tap on image inside WKWebView

So I have managed to surpass this issue.

Somehow I didn't knew I could create a link to a locally cached image as @GIJOW said.

After reading about it here I have managed to display an image inside the WKWebView as it was before, but the whole image is now tappable because it is embeded in a <a href> tag as the following.

Previous

<img src=\"\(imageNameHere)\"/>

After

<a href=\"some_url_or_link_you_want\"><img src=\"\(imageNameHere)\"/></a>

Detect that user tap on screen Swift

Add UIGestureRecognizer to the super view :

As you said you have image view as a background.

 let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapped(gestureRecognizer:)))
imageView.addGestureRecognizer(tapRecognizer)
tapRecognizer.delegate = self

Adding target action :

func tapped(gestureRecognizer: UITapGestureRecognizer) {
// Remove the blue view.
}

And then inside UITapGestureRecognizerDelegate :

extension ViewController : UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
if touch.view!.superview!.superclass! .isSubclass(of: UIButton.self) {
return false
}
return true
}
}

Hope it helps !

EDIT

Make sure that user can touch on the view by enabling : self.view.userInteractionEnabled = true

How to find what image is displayed in UIImageView swift

You can get to the UIImage object like so:

func imageTapped(gestureRecognizer: UITapGestureRecognizer) {
if let tappedImageView = gestureRecognizer.view as? UIImageView {
print(tappedImageView.image)
}
}


Related Topics



Leave a reply



Submit