Reputation: 5427
I just have created two moving/draggable UIImageView
which are supposed to move on a larger UIImageView
below, like the following screenshot:
And this is my code:
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)")
}
...
}
As you can imagine when I tap on a pin then also the other is moved into the position of the first and this, I think, because I always perform the same extraction from touches: Set<UITouch>
.
What is the correct way to manage both pin without the position of the first is overwritten on the position of the second at each tapping?
Thank you
Upvotes: 1
Views: 357
Reputation: 354
If you want to press and drag an imageView around you should add a panGestureRecognizer
to each imageView
Upvotes: 1