tl;dr : how should I set my tags so that they can be retrieved with the gesture recognizer ?
I'm setting a view in which the user can spawn multiple UIImageViews when he presses a button.
The image creation process is :
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)
Then the user can move the created images using the Gesture Recognizer's fonction "handlePan" below :
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 < 100 {
switchRang.setOn(true, animated: true)
println("dans switch if")
} else {
switchRang.setOn(false, animated: true)
println("dans switch else")
}
}
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 how to retrieve the tag of the image that is currently being moved by the user.
I tried this solution here, but it's not the image's tag as it always returns 0 even if the tag is different.
So, my question is : how should I set my tags so that they can be retrieved with the gesture recognizer ?
I'm deeply stuck here, so any help would be greatly appreciated !
Thanks
Update 1 :
Thanks to Rdelmar, I've been able to move forward… but not too far !
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.
The code is :
var siegeView: UIView!
var rond1: UIImageView!
var rond2: UIImageView!
var setTag : Int!
var tagCounter = 1
var tagInfo = 0
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 < 100 {
switchRang.setOn(true, animated: true)
println("dans switch if")
} else {
switchRang.setOn(false, animated: true)
println("dans switch else")
}
}
and the showcontent fonction updated :
@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)
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 var centerRondY = rond1.viewWithTag(tagInfo)!.center.y
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 !
Best Answer
I think this code does what you want. I commented some stuff out, since I didn't know what it was, and I hard coded the image and text in the label. As I said in my comment, you don't need to use tags, since each recognizer knows its own view. I commented out the button stuff, but I do see the "if" and "else" log statements fire as I move the views up and down the screen,