如何限制图像视图的拖动区域

How do I limit the dragging area for image views

我的第一个 post,我目前正在使用 Swift 3

在 Xcode 8.1 中制作应用程序

我使用 touchesBegan 和 touchesMoved 函数将 9 张图像设为可拖动。

然而,它们可以被拖到屏幕上的任何地方,这可能会导致它们掩盖我拥有的其他图像。我想通过为它们设置边界来限制它们的移动,这样即使用户试图将图像拖出该边界,它们也无法做到。

我在 draggedimageview.swift 中创建了这段代码,它允许拖动图像视图。

我花了很长时间试图弄清楚如何做到这一点,如果有人能提供帮助,我将不胜感激。

谢谢...

import UIKit

class DraggedImageView: UIImageView {

    var startLocation: CGPoint?

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        startLocation = touches.first?.location(in: self)
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

        let currentLocation = touches.first?.location(in: self)
        let dx = currentLocation!.x - startLocation!.x
        let dy = currentLocation!.y - startLocation!.y

        self.center = CGPoint(x: self.center.x+dx, y: self.center.y+dy)
    }
}

你可以这样做:

let cx = self.center.x+dx
if (cx > 100) {
   cx = 100
}

self.center = CGPoint(x: cx, y: self.center.y+dy)

但根据您要执行的操作更改 if。这会夹住它,使其无法移动到 center.x > 100

的位置

尝试把你的"allowed area"定义在一个rect中,比如:

import UIKit

class DraggedImageView: UIImageView {

    var startLocation: CGPoint?

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        startLocation = touches.first?.location(in: self)
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

        let currentLocation = touches.first?.location(in: self)
        let dx = currentLocation!.x - startLocation!.x
        let dy = currentLocation!.y - startLocation!.y

        // This is the area in which the dragging is allowed
        let coolArea = CGRect(x: 0, y: 0, width: 100, height: 100)

        let newCenter = CGPoint(x: self.center.x+dx, y: self.center.y+dy)

        // If the allowed area contains the new point, we can assign it
        if coolArea.contains(newCenter) {
            self.center = newCenter
        }
        // else {
        //    print("Out of boundaries!")
        // }

        self.center = CGPoint(x: self.center.x+dx, y: self.center.y+dy)
    }
}

如果您希望在用户拖出边界时发生不同的事情,您可能需要更改代码。

从包含 UIImageView

的视图中获取 "allowed area"
import UIKit

class DraggedImageView: UIImageView {

    var startLocation: CGPoint?

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        startLocation = touches.first?.location(in: self)
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

        let currentLocation = touches.first?.location(in: self)
        let dx = currentLocation!.x - startLocation!.x
        let dy = currentLocation!.y - startLocation!.y

        let coolArea = (self.superview?.bounds)!

        let newCenter = CGPoint(x: self.center.x+dx, y: self.center.y+dy)

        // If the allowed area contains the new point, we can assign it
        if coolArea.contains(newCenter) {
            self.center = newCenter
            print("touchesMoved")
        }
         else {
            print("Out of boundaries!")
         }
    }
}