我刚创建了两个移动/可拖动的UIImageView
,它们应该在下方更大的UIImageView
上移动,如下面的屏幕截图所示:
这是我的代码:
class PhotoViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var redA: UIImageView!
@IBOutlet weak var redB: UIImageView!
var imagePicker: UIImagePickerController!
var redLocationA:CGPoint = CGPoint(x:0, y:0)
var redLocationB:CGPoint = CGPoint(x:0, y:0)
override func viewDidLoad() {
super.viewDidLoad()
self.redA.center = CGPointMake(80, 330)
self.redB.center = CGPointMake(80, 360)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.redLocationA = (touches.first?.locationInView(self.view))!
self.redA.center = redLocationA
self.redLocationB = (touches.first?.locationInView(self.view))!
self.redB.center = redLocationB
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.redLocationA = (touches.first?.locationInView(self.view))!
if ((redLocationA.x >= 26 && redLocationA.x <= 292) && (redLocationA.y >= 71 && redLocationA.y <= 461)){
redA.center = redLocationA
}
if ((redLocationB.x >= 26 && redLocationB.x <= 292) && (redLocationB.y >= 71 && redLocationB.y <= 461)){
redB.center = redLocationB
}
print("redA: \(redLocationA)")
print("redB: \(redLocationB)")
}
...
}
你可以想象当我点击一个别针然后另一个被移动到第一个的位置,我认为,因为我总是从touches: Set<UITouch>
执行相同的提取。
在每次攻丝时,在没有第一个位置的情况下管理两个引脚的正确方法是什么?
谢谢
答案 0 :(得分:1)
如果你想按下并拖动图像,你应该为每个imageView添加panGestureRecognizer