swift - 在 iOS 中移动多个图像

标签 swift imageview touch

我在 View Controller 中有多个图像。我正在尝试实现触摸方法来拖动图像。当我尝试拖动单个 ImageView 时,所有 ImageView 都会被拖动。这是我正在使用的代码:

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

相关文章:

iOS swift 4 为复杂的静态变量设置默认 bool 值?

ios - 在 Swift 中替换指示符之间的子字符串的更好方法?

android - 适合图像宽度以匹配父级并根据 ImageView 中的纵横比调整高度

javascript - 有没有JS触屏 Controller 库?

swift - 在 Swift 2.2 中,如何合并 for 循环语句?

swift - 远程图像内容错误快速错误

java - setImageRessource 在计时器 android studio 中不起作用

image - 在 javafx 中创建图像覆盖掩码

ios - 如果 UIGestureRecognizer 触发,如何取消按钮点击?

c# - 如何在 Windows Phone 中禁用多点触控?