【发布时间】:2016-08-22 22:00:32
【问题描述】:
我在视图控制器中有多个图像。我正在尝试实现触摸方法来拖动图像。当我尝试拖动单个图像视图时,所有图像视图都在拖动。这是我正在使用的代码:
@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
}
}
}
【问题讨论】:
-
这段代码很好,应该可以工作。检查您的图像视图是否有多个出口。
-
是的..每个图像视图都有一个出口..
-
而且我还没有在 touchMoved 方法中添加 super.touchesBegan(touches, withEvent: event) ..
-
无法弄清楚如何完成这项工作...任何帮助.???
-
尝试了所有备用代码..但有时所有图像都开始移动,否则图像没有被点击..