Я нашел решение или на самом деле, что я делал неправильно:
Вам нужно определить новую переменную UIPanGestureRecognizer
для каждого UIImage
, переменная управляет одним объектом за раз.
Спасибо
Вот что сработало
@IBOutlet var imagesToMove: [UIImageView]!
let imagesNames = ["chk", "unchk"]
var gesturePanArray: [UIPanGestureRecognizer] = []
var gestureTapArray: [UITapGestureRecognizer] = []
override func viewDidLoad() {
super.viewDidLoad()
var i = 0
for imageItem in imagesToMove {
imageItem.image = UIImage(named: imagesNames[0])
imageItem.isUserInteractionEnabled = true
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.wasDragged(_:)))
gesturePanArray.append(panGesture)
imageItem.addGestureRecognizer(gesturePanArray[i])
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.wasTouched(_:)))
gestureTapArray.append(tapGesture)
imageItem.addGestureRecognizer(gestureTapArray[i])
i = i + 1
}
}
@objc func wasDragged(_ gesture: UIPanGestureRecognizer) {
let translation = gesture.translation(in: self.view)
let image = gesture.view
view.bringSubviewToFront(image!)
image!.center = CGPoint(x: (image?.center.x)! + translation.x, y: (image?.center.y)! + translation.y)
gesture.setTranslation(CGPoint.zero, in: self.view)
}
@objc func wasTouched (_ gesture: UITapGestureRecognizer) {
let imageTag = gesture.view?.tag
if imagesToMove[imageTag! - 1].image == UIImage(named: imagesNames[0]) {
imagesToMove[imageTag! - 1].image = UIImage(named: self.imagesNames[1])
} else {
imagesToMove[imageTag! - 1].image = UIImage(named: self.imagesNames[0])
}
}
}