【问题标题】:Move Multiple images in iOS在 iOS 中移动多个图像
【发布时间】: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) ..
  • 无法弄清楚如何完成这项工作...任何帮助.???
  • 尝试了所有备用代码..但有时所有图像都开始移动,否则图像没有被点击..

标签: swift imageview touch


【解决方案1】:

您需要获取 UIimageView 数组,然后设置识别器。因此,每个识别器都将在 NSUser Default 中设置。

【讨论】:

  • 不能用上面提到的代码实现吗??
  • 没有。您必须将一个 colletionview 水平放置并在 didSelect Action 或任何操作 addsubview 上查看。然后,您需要将该 imageViewview 附加到 imageView 数组中。这就是逻辑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多