在iOS中移动多张图片

在iOS中移动多张图片

我在视图控制器中有多个图像。我正在尝试实现touch方法来拖动图像。当我尝试拖动单个图像视图时,所有图像视图都在拖动。这是我正在使用的代码:

@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
        }
    }
}

最佳答案

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

关于swift - 在iOS中移动多张图片,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39078488/

10-13 03:27