Find Sender Tag with Gesture Recognizer and Fast
tl; dr: how do I set tags so that they can be recovered with gesture recognizer?
I am setting up a view where the user can invoke multiple UIImageViews when they click a button. Image creation process:
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)
The user can then move the generated images with the Fonction Recorizer Gearure "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 at this point is to be able to get DistanceCenterY information, and an if operation should be active for every view the user moves. But it only works for the last created view. I guess it might work if I specify an image tag. But I can't figure out how to get the tag of the image that the user is currently navigating .
I tried it here but it is not an image tag as it always returns 0 even if the tag is different.
So my question is, how do I set the tags so that they can be found using the gesture recognizer?
I am deeply stuck here, so any help would be greatly appreciated! Thanks to
Update 1: Thanks to Rdelmar, I was able to move forward ... but not too far!
I updated the way the image was generated and changed the Gesture Recognizer setting to use the tag information to get information about which image is selected.
Code:
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 updated highlighting showcontent:
@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 returned tag is correct when I move the image, but when I add the second image and I move the first one, I get a fatal error because zero was returned for the row var centerRondY = rond1.viewWithTag(tagInfo)!.center.y
I am still stuck because I cannot find what happened. It is not clear to me how the recognizer works. If you have any hints, it is still greatly appreciated. Thank!
source to share
I think this code does what you want. I commented on some things as I didn't know what it was and I hardcoded the image and text in the label. As I said in my comment, you don't need to use tags as each recognizer knows its own representation. I have commented out the button stuff, but I can see that the "if" and "else" log statements fire when the views move up and down the screen,
class ViewController: UIViewController {
@IBAction func showContent(sender: AnyObject) {
var rond1 = UIImageView(frame: CGRectMake(0, 0, 100, 100))
rond1.image = UIImage(named:"Lofoten.jpg")
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 = "Picture"
label.textColor = UIColor.whiteColor()
label.frame = CGRectMake(0, rond1.frame.size.height - 25, 100, 25)
rond1.addSubview(label)
rond1.userInteractionEnabled = true
view.addSubview(rond1)
let recognizer = UIPanGestureRecognizer(target: self, action:Selector("handlePan:"))
rond1.addGestureRecognizer(recognizer)
}
func handlePan(recognizer:UIPanGestureRecognizer) {
let iv = recognizer.view
let translation = recognizer.translationInView(self.view)
iv.center.x += translation.x
iv.center.y += translation.y
recognizer.setTranslation(CGPointZero, inView: self.view)
// var switchRang = premierRang
var centerBoardY = self.view.center.y
var centerRondY = iv.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")
}
}
}
source to share