【问题标题】:How do I limit the dragging area for image views如何限制图像视图的拖动区域
【发布时间】:2017-08-26 05:03:28
【问题描述】:

我的第一篇文章,我目前正在使用 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)
    }
}

【问题讨论】:

  • 在设置 self.center 之前,先看看你要设置什么,然后根据你的边界改变它。向我们展示你的尝试。
  • 优秀的第一篇文章!继续努力。

标签: ios swift draggable


【解决方案1】:

你可以这样做:

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

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

但是根据你想要做的改变 if 。这会将其夹住,使其无法移动到center.x &gt; 100

【讨论】:

  • 谢谢,我需要把它放在我写的当前代码的什么地方才能让它工作?还有cx指的是什么?我对 Xcode 和一般编程非常陌生。
  • 您通常在哪里设置中心。在您显示的代码中,它在 touchesMoved
  • 我是否必须使用此代码而不是我使用的某些代码或补充?
  • 我尝试使用该代码,它似乎确实对其进行了限制,但仅将其限制在我认为的中心左侧的屏幕左侧
  • 我给你一个例子——你需要根据例子完成它。 if cx &gt; 100 表示 center.x 是否大于 100。如果是,则将其设置为 100。这意味着如果您从 100 向左移动,它会在 100 处停止。现在,为右侧编写等效值。对于顶部和底部,您需要制作一个为 center.y + dy 的 cy 并执行类似的操作
【解决方案2】:

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

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

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

【讨论】:

  • 那么我需要将此代码添加到我当前的代码中吗?我只是在努力理解这段代码的实际作用。
  • 那么我需要将此代码添加到我当前的代码中吗?我只是在努力理解这段代码的实际作用。 coolArea 将是我想要将图像保留在其中的矩形边界,不是吗。那么我需要在我当前的代码之上编写这个还是需要更改一些?
  • 完善了例子,看看
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 2011-03-15
    相关资源
    最近更新 更多