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

时间:2017-03-31 20:08:38

标签: ios swift draggable

我的第一篇文章,我目前正在使用Swift 3在Xcode 8.1中制作应用程序

我有9张图片,我用touchesBegan和touchesMoved函数进行了拖动。

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

我在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)
    }
}

3 个答案:

答案 0 :(得分:3)

你可以这样做:

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

的位置

答案 1 :(得分:1)

尝试在矩形中定义“允许区域”,例如:

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

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

答案 2 :(得分:0)

从包含UIImageView的视图中获取“允许区域”

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!")
         }
    }
}