Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get UIImage from sender in swift

I have a UIImageView inside a table cell and I added a tapGesture to it. I want to access the UIImageView in the handleTap method.

This is the code for the Image inside the TableCell :

func setImageForCell(cell:ImageCell, indexPath:NSIndexPath) {
    var image : UIImage = UIImage(named: "brunnen1")!

    cell.customImageView.userInteractionEnabled = true
    cell.imageView!.tag = indexPath.row;
    var tapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector("handleTap:"))
    tapGestureRecognizer.numberOfTapsRequired = 1;
    cell.customImageView.addGestureRecognizer(tapGestureRecognizer)
    cell.customImageView.image = image
}


func handleTap(sender : UIView) {
    // get the UIImageview from the sender, i guess ?

}

I guess I have to cast it from the UIView?

like image 335
plasmid0h Avatar asked Jul 10 '15 01:07

plasmid0h


1 Answers

Try this code.

func handleTap(sender : UITapGestureRecognizer) {
    let imgView = sender.view as! UIImageView
    // Do something.
}

Since you use a gesture recognizer, handleTap's sender will be UITapGestureRecognizer. The gesture recognizer has what you want.

var view: UIView? { get } // the view the gesture is attached to. set by adding the recognizer to a UIView using the addGestureRecognizer: method

like image 194
Wonjung Kim Avatar answered Nov 05 '22 06:11

Wonjung Kim