title: Find sender's tag with gesture recognizer and swift
body: <p>tl;dr :  <strong>how should I set my tags so that they can be retrieved with the gesture recognizer ?</strong> </p>
  
  <p>I'm setting a view in which the user can spawn multiple UIImageViews when he presses a button. 
  The image creation process is : </p>
  
  <pre><code>var siegeView: UIView!
  var round1: UIImageView!
  var setTag : Int!
  var tagCounter = 0
  
   @IBAction func showContent(sender: AnyObject) {
  
  
      round1 = UIImageView(frame: CGRectMake(0, 0, 100, 100))
  
      round1.image = UIImage(named: nomDuRond.text)
  
      setTag = tagCounter
      tagCounter++
      self.rond1.tag = setTag
  
      var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
      label.center = CGPointMake(round1.frame.origin.x+50, round1.frame.origin.y+100)
      label.textAlignment = NSTextAlignment.Center
      label.text = nomDuRond.text
  
      siegeView = UIView(frame: CGRectMake(round1.frame.origin.x, round1.frame.origin.y, round1.frame.size.width, round1.frame.size.height))
  
      round1.userInteractionEnabled = true
      siegeView.addSubview(rond1)
      siegeView.addSubview(label)
  
      view.addSubview(siegeView)
  
      let recognizer = UIPanGestureRecognizer(target: self, action:Selector("handlePan:"))
      recognizer.delegate = ClassSiege()
      siegeView.addGestureRecognizer(recognizer)
  </code></pre>
  
  <p>Then the user can move the created images using the Gesture Recognizer's fonction "handlePan" below : </p>
  
  <pre><code> func handlePan(recognizer:UIPanGestureRecognizer) {
      let translation = recognizer.translationInView(self.view)
      self.view.bringSubviewToFront(recognizer.view!)
      recognizer.view!.center = CGPoint(x:recognizer.view!.center.x + translation.x,
          y:recognizer.view!.center.y + translation.y)
      recognizer.setTranslation(CGPointZero, inView: self.view)
  
      var centerBoardX = BlackBoard.center.x
      var centerBoardY = BlackBoard.center.y
      var centerRondX = round1.superview?.center.x
      var centerRondY = round1.superview?.center.y
      var switchRang = premierRang
  
      DistanceCenterY.text = " \(centerRondY! - centerBoardY)"
  
  
      if centerRondY! - centerBoardY &lt; 100 {
          switchRang.setOn(true, animated: true)
  
          println("dans switch if")
      } else {
  
          switchRang.setOn(false, animated: true)
          println("dans switch else")
      }
  
  }
  </code></pre>
  
  <p>My goal, for now, is to be able to get the DistanceCenterY information, and the if operation to be active for each view the user is moving. But it's only working for the last view created. 
  My guess is that it can work if I specify the image's tag. 
  But I can't figure out <strong>how to retrieve the tag of the image that is currently being moved by the user</strong>. </p>
  
  <p>I tried this <a href="https://stackoverflow.com/questions/17389683/get-index-or-tag-value-from-imageview-tap-gesture">solution here</a>, but it's not the image's tag as it always returns 0 even if the tag is different. </p>
  
  <p>So, my question is : <strong>how should I set my tags so that they can be retrieved with the gesture recognizer ?</strong> </p>
  
  <p>I'm deeply stuck here, so any help would be greatly appreciated !
  Thanks</p>
  
  <p><strong>Update 1 :</strong> 
  Thanks to Rdelmar, I've been able to move forward... but not too far ! </p>
  
  <p>I updated the way I create the image, and modified the Gesture Recognizer fonction to use the tag info to get the info of which image is selected. </p>
  
  <p>The code is : </p>
  
  <p>var siegeView: UIView!
      var rond1: UIImageView!
      var rond2: UIImageView!
      var setTag : Int!
      var tagCounter = 1
      var tagInfo = 0</p>
  
  <pre><code>  func handlePan(recognizer:UIPanGestureRecognizer) {
      let translation = recognizer.translationInView(self.view)
      recognizer.view!.center = CGPoint(x:recognizer.view!.center.x + translation.x,
          y:recognizer.view!.center.y + translation.y)
      recognizer.setTranslation(CGPointZero, inView: self.view)
  
  
      var switchRang = premierRang
      //Here I'm getting the tag from the recognizer. 
      var tag = recognizer.view?.tag
      tagInfo = tag!
  
  
      var centerBoardX = BlackBoard.center.x
      var centerBoardY = BlackBoard.center.y
      //to get the coordinates of the image, i'm getting the info using the tag I got earlier. 
      var centerRondX = rond1.viewWithTag(tagInfo)!.center.x
      var centerRondY = rond1.viewWithTag(tagInfo)!.center.y
  
      DistanceCenterY.text = " \(centerRondY - centerBoardY)"
  
  
      if centerRondY - centerBoardY &lt; 100 {
          switchRang.setOn(true, animated: true)
  
          println("dans switch if")
      } else {
  
          switchRang.setOn(false, animated: true)
          println("dans switch else")
      }
  
  }
  </code></pre>
  
  <p>and the showcontent fonction updated : </p>
  
  <pre><code>@IBAction func showContent(sender: AnyObject) {
  
  
      rond1 = UIImageView(frame: CGRectMake(0, 0, 100, 100))
  
      rond1.image = UIImage(named: nomDuRond.text)
  
      setTag = tagCounter
      tagCounter++
      rond1.tag = setTag
  
      var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
      label.center = CGPointMake(rond1.frame.origin.x+50, rond1.frame.origin.y+100)
      label.textAlignment = NSTextAlignment.Center
      label.text = nomDuRond.text
  
      rond1.userInteractionEnabled = true
      view.addSubview(rond1)
      //the label subview was getting me some bugs, so for now I removed it. 
      //view.addSubview(label)
  
      let recognizer = UIPanGestureRecognizer(target: self, action:Selector("handlePan:"))
      recognizer.delegate = ClassSiege()
      rond1.addGestureRecognizer(recognizer)
  </code></pre>
  
  <p><strong>The tag returned is correct when I move the image, but when I adding a second image and I'm moving the first one, I get a fatal error because a nil was returned for the line</strong> <code>var centerRondY = rond1.viewWithTag(tagInfo)!.center.y</code></p>
  
  <p>I'm still stuck because I can't find what's wrong. It's not clear to me how the recognizer is working. If you have some clues, it's still very very much appreciated. Thanks !</p>