tl; dr:我应该如何设置我的标签,以便可以使用手势识别器检索它们?
我设置了一个视图,用户可以在按下按钮时生成多个UIImageViews。 图像创建过程是:
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)
然后,用户可以使用手势识别器的功能移动创建的图像" handlePan"下面:
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")
}
}
目前,我的目标是能够获取DistanceCenterY信息,并且if操作对于用户正在移动的每个视图都是活动的。但它只适用于最后创建的视图。 我的猜测是,如果我指定图像的标签,它可以工作。 但我无法弄清如何检索用户当前正在移动的图片的标记。
我试过这个solution here,但它不是图片的标签,因为它总是返回0,即使标签不同。
所以,我的问题是:我应该如何设置我的标签,以便可以使用手势识别器检索它们?
我深深地被困在这里,所以任何帮助都将不胜感激! 感谢
更新1: 感谢Rdelmar,我已经能够向前迈进......但不要太远!
我更新了创建图像的方式,并修改了Gesture Recognizer功能以使用标记信息来获取选择了哪个图像的信息。
代码是:
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")
}
}
并更新了showcontent fonction:
@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)
移动图片时返回的标签是正确的,但是当我添加第二张图片并且移动第一张图片时,我收到致命错误,因为该行返回了nil var centerRondY = rond1.viewWithTag(tagInfo)!.center.y
我仍然坚持,因为我无法找到错误。我不清楚识别器是如何工作的。如果你有一些线索,我们仍然非常感激。谢谢!
答案 0 :(得分:4)
我认为这段代码可以满足您的需求。我评论了一些东西,因为我不知道它是什么,我在标签上硬编码了图像和文字。正如我在评论中所说,你不需要使用标签,因为每个识别器都知道自己的观点。我评论了按钮的东西,但我确实看到&#34; if&#34;和&#34;否则&#34;当我在屏幕上上下移动视图时,日志语句会激活,
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")
}
}
}