Get UIImage from sender in fast

I have a UIImageViewtable cell inside and I added tapGesture. I want to access UIImageViewin 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 ?

}

Think I need to drop it from UIView?

+4
source share
2 answers

Try this code.

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

Since you are using a gesture recognizer, the sender handleTapwill be UITapGestureRecognizer. The gesture recognizer has what you want.

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

+7
source

, sender UITapGestureRecognizer not UIView. :

func handleTap(sender : UITapGestureRecognizer) {
    let imageView = sender.view as! UIImageView
}
0

All Articles