How to assign an action for UIImageView object in Swift

uiimageview click event swift 4
add gesture recognizer to uiimageview storyboard
uiimageview tap gesture not working swift
add tap gesture recognizer to uiview swift 4
swift button action
swift uiimageview touchesbegan
uitapgesturerecognizer
uitapgesturerecognizer swift 4

I'm trying to assign an UIImageView to an action when the user taps it.

I know how to create an action for a UIButton, but how could I mimic the same behavior of a UIButton, but using a UIImageView?

You'll need a UITapGestureRecognizer. To set up use this:

override func viewDidLoad()
{
    super.viewDidLoad()

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
    imageView.isUserInteractionEnabled = true
    imageView.addGestureRecognizer(tapGestureRecognizer)
}

@objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
    let tappedImage = tapGestureRecognizer.view as! UIImageView

    // Your action
}

(You could also use a UIButton and assign an image to it, without text and than simply connect an IBAction)

How to assign or add an action for UIImageView object in Swift 5 , }func imageTapped(tapGestureRecognizer: UITapGestureRecognizer) { let tappedImage = tapGestureRecognizer.view as! UIImageView // And some actions }. Questions: I’m trying to assign an UIImageView to an action when the user taps it. I know how to create an action for a UIButton, but how could I mimic the same behavior of a UIButton, but using a UIImageView? Answers: You’ll need a UITapGestureRecognizer. To set up use this: override func viewDidLoad() { super.viewDidLoad()

You need to add a a gesture recognizer (For tap use UITapGestureRecognizer, for tap and hold use UILongPressGestureRecognizer) to your UIImageView.

let tap = UITapGestureRecognizer(target: self, action: #selector(YourClass.tappedMe))
imageView.addGestureRecognizer(tap)
imageView.isUserInteractionEnabled = true

And Implement the selector method like:

@objc func tappedMe()
{
    println("Tapped on Image")
}

Swift: Clickable UIImageView with Extension, I'm trying to assign an UIImageView to an action when the user taps it. I know how to create an action for a UIButton, but how could I mimic the same behavior of  To set up use this: override func viewDidLoad(){ { super.viewDidLoad() var imageView = <# imageView #> var tapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector Ios - How to assign an action for UIImageView object in Swift

You can add a UITapGestureRecognizer to the imageView, just drag one into your Storyboard/xib, Ctrl-drag from the imageView to the gestureRecognizer, and Ctrl-drag from the gestureRecognizer to the Swift-file to make an IBAction.

You'll also need to enable user interactions on the UIImageView, as shown in this image:

UIImage, UIImage. Language: Swift, Objective-C. Language: Swift; Objective-C An image object may contain a single image or a sequence of images you intend to use in an animation. Assign an image to a UIImageView object to display the image in your interface. The standard image for indicating user-initiated actions​. in above code var imageViewSlot = UIImageView() you gave this as global, and you used same name in for in statement that is the main problem. So because of this it donot know which type of object this is, So for that we have to mention like this (imageViewSlot as UIImageView).image = image

For swift 2.0 and above do this

@IBOutlet weak var imageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tappedMe))
        imageView.addGestureRecognizer(tap)
        imageView.userInteractionEnabled = true
    }
    func tappedMe()
    {
        print("Tapped on Image")
    }

UIImageView, An object that displays a single image or a sequence of animated images in your You specify an image sequence as an array of UIImage objects and assign  Programmatically set image to UIImageView with Xcode 6.1/Swift have it already connected to a UIImageView object on a storyboard/xib file, then you could so

Swift4 Code

Try this some new extension methods:

import UIKit

extension UIView {

    fileprivate struct AssociatedObjectKeys {
        static var tapGestureRecognizer = "MediaViewerAssociatedObjectKey_mediaViewer"
    }

    fileprivate typealias Action = (() -> Void)?


    fileprivate var tapGestureRecognizerAction: Action? {
        set {
            if let newValue = newValue {
                // Computed properties get stored as associated objects
                objc_setAssociatedObject(self, &AssociatedObjectKeys.tapGestureRecognizer, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
            }
        }
        get {
            let tapGestureRecognizerActionInstance = objc_getAssociatedObject(self, &AssociatedObjectKeys.tapGestureRecognizer) as? Action
            return tapGestureRecognizerActionInstance
        }
    }


    public func addTapGestureRecognizer(action: (() -> Void)?) {
        self.isUserInteractionEnabled = true
        self.tapGestureRecognizerAction = action
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture))
        self.addGestureRecognizer(tapGestureRecognizer)
    }


    @objc fileprivate func handleTapGesture(sender: UITapGestureRecognizer) {
        if let action = self.tapGestureRecognizerAction {
            action?()
        } else {
            print("no action")
        }
    }

}

Now whenever we want to add a UITapGestureRecognizer to a UIView or UIView subclass like UIImageView, we can do so without creating associated functions for selectors!

Usage:

 profile_ImageView.addTapGestureRecognizer {
        print("image tapped")
    }

Making UIImageView Clickable Using UITapGestureRecognizer, let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self. 2- Now add above created instance to UIImageView object. of us usually forget to make user interaction enabled for the view to which we are assigning the UITapGesture. UITapGestureRecognizer Swift UIImageView Tappable iPhone SDK. Next, drag a UITableViewController object from the Object library to the canvas, then follow the steps below: – Select the Table View Cell from the Document Outline view, then switch to the Attributes inspector and set the Identifier property to “CellID”.

Uiimageview zoom swift, Oct 06, 2018 · Pinch To Zoom - UIImageView - Swift UIScrollView Zooming Nov 27, 2017 · Home » Ios » How to assign an action for UIImageView object in  Rather than assigning a large image to an image view, created a scaled version that matches the current size of the image view. You can also create a resizable image object using the UIImage.Resizing Mode.tile option, which tiles the image instead of scaling it. Make your image view opaque whenever possible.

How do I register a tap on UIImage view which is inside , In that case I can't make the action change the data arrray the tableview feeds I know probably most of that stuff is like low level C not even Swift but I would  Questions: I am trying to set an Image for bar button Item for that I have an image like: with resolution 30 * 30 but while I assign this Image to Bar button Its looks like: I have assigned image this way : and If I try this way like making an IBOutlet for the

Using Gesture Recognizers, This is a quick guide for the steps in setting up gesture recognizers. Choose a Gesture Recognizer from the Object Library based on the Step 3: Create an Action for your Gesture Recognizer. Ctrl-drag from your Gesture Recognizer to the ViewController swift file to UIImageView imageView.transform  Hi, Developers. I knew it’s bit weird sound. Most of developers didn’t find a proper solution for applying action on UIImageview Object in Swift Earlier and current versions. I come up with Perfect working solution, all u need to do is add Tap Gesture with method to your UIImageview Object.

Comments
  • Nitpick: var tgr = UITapGestureRecognizer(target:self action:Selector("imageTapped:")), need a comma between the two parameters.
  • as stated below by MidHun MP, make sure you have the userInteractionEnabled = true
  • In Swift 3.0 the syntax of action:Selector("imageTapped:") changed to action: #selector(imageTapped))
  • Also in Swift 3.0 the userInteractionEnabled property has been renamed to isUserInteractionEnabled
  • Using Swift 4, this worked for me, but I had to add an @objc modifier to the function.
  • userInteractionEnabled is key.
  • @hatim: What is the issue you are facing ? If you have a new issue, please post it as a new question.
  • @MidhunMP I am generating the image view dynamically and have done exactly what is written in this answer but tappedMe() function is not getting called
  • It worked for me with the following changes: let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tappedMe)) // next line -> successIndicatorImg.addGestureRecognizer(tap) // next line -> successIndicatorImg.isUserInteractionEnabled = true
  • @hatim are you using same gesture to multiple UI views ? I just solved this issue as i was using same gesture for multiple views and only one was working for me. when i create multiple gestures it worked for me.
  • UIImageView doesn't respond to touches by default. You also have to enable user interactions.
  • @rmaddy Oops, that's true. Edited.
  • I like how you can set this up using the IB, drag/drop is powerful, and didn't expect it was possible. +1 for that. As a noob, it would be easy to not know how it's connected without "mousing around" the storyboard. It depends on your preference, but I prefer to "see it in code".