我在视图控制器中有多个图像。我正在尝试实现触摸方法来拖动图像。当我尝试拖动单个图像视图时,所有图像视图都在拖动。这是我正在使用的代码:
@IBOutlet weak var img1: UIImageView!
@IBOutlet weak var img2: UIImageView!
@IBOutlet weak var img3: UIImageView!
@IBOutlet weak var img4: UIImageView!
var location = CGPoint(x: 0 , y:0)
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first{
location = touch.locationInView(self.view)
if touch.view == img1 {
print("img1 tapped")
img1.center = location
}
else if touch.view == img2 {
print("img2 tapped")
img2.center = location
}
else if touch.view == img3 {
print("img3 tapped")
img3.center = location
}
}
super.touchesBegan(touches, withEvent: event)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first{
location = touch.locationInView(self.view)
if touch.view == img1 {
print("img1 tapped")
img1.center = location
}
else if touch.view == img2 {
print("img2 tapped")
img2.center = location
}
else if touch.view == img3 {
print("img3 tapped")
img3.center = location
}
}
}
答案 0 :(得分:0)
您需要获取UIimageView数组,然后设置识别器。因此,每个识别器都将设置为NSUser Default。